add service
All checks were successful
Deploy Smart Search Backend Test / deploy (push) Successful in 1m44s
All checks were successful
Deploy Smart Search Backend Test / deploy (push) Successful in 1m44s
This commit is contained in:
@@ -153,30 +153,33 @@ func (r *requestRepository) GetByID(ctx context.Context, id uuid.UUID) (*model.R
|
||||
}
|
||||
|
||||
func (r *requestRepository) GetDetailByID(ctx context.Context, id uuid.UUID) (*model.RequestDetail, error) {
|
||||
sqlQuery := `
|
||||
SELECT
|
||||
r.id AS request_id,
|
||||
r.request_txt AS title,
|
||||
r.final_update_tz AS mail_text,
|
||||
COALESCE(json_agg(
|
||||
json_build_object(
|
||||
'email', COALESCE(s.email, ''),
|
||||
'phone', COALESCE(s.phone, ''),
|
||||
'company_name', COALESCE(s.name, ''),
|
||||
'company_id', s.id,
|
||||
'url', COALESCE(s.url, '')
|
||||
)
|
||||
) FILTER (WHERE s.id IS NOT NULL), '[]') AS suppliers
|
||||
FROM requests_for_suppliers r
|
||||
LEFT JOIN suppliers s ON s.request_id = r.id
|
||||
WHERE r.id = $1
|
||||
GROUP BY r.id, r.request_txt, r.final_update_tz
|
||||
`
|
||||
query := r.qb.Select(
|
||||
"r.id AS request_id",
|
||||
"r.request_txt AS title",
|
||||
"r.final_update_tz AS mail_text",
|
||||
`COALESCE(json_agg(
|
||||
json_build_object(
|
||||
'email', COALESCE(s.email, ''),
|
||||
'phone', COALESCE(s.phone, ''),
|
||||
'company_name', COALESCE(s.name, ''),
|
||||
'company_id', s.id,
|
||||
'url', COALESCE(s.url, '')
|
||||
)
|
||||
) FILTER (WHERE s.id IS NOT NULL), '[]') AS suppliers`,
|
||||
).From("requests_for_suppliers r").
|
||||
LeftJoin("suppliers s ON s.request_id = r.id").
|
||||
Where(sq.Eq{"r.id": id}).
|
||||
GroupBy("r.id", "r.request_txt", "r.final_update_tz")
|
||||
|
||||
sqlQuery, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
return nil, errs.NewInternalError(errs.DatabaseError, "failed to build query", err)
|
||||
}
|
||||
|
||||
detail := &model.RequestDetail{}
|
||||
var suppliersJSON []byte
|
||||
|
||||
err := r.pool.QueryRow(ctx, sqlQuery, id).Scan(
|
||||
err = r.pool.QueryRow(ctx, sqlQuery, args...).Scan(
|
||||
&detail.RequestID, &detail.Title, &detail.MailText, &suppliersJSON,
|
||||
)
|
||||
|
||||
@@ -197,17 +200,20 @@ func (r *requestRepository) GetDetailByID(ctx context.Context, id uuid.UUID) (*m
|
||||
}
|
||||
|
||||
func (r *requestRepository) GetUserStatistics(ctx context.Context, userID int) (requestsCount, suppliersCount, createdTZ int, err error) {
|
||||
sqlQuery := `
|
||||
SELECT
|
||||
COUNT(DISTINCT r.id) AS requests_count,
|
||||
COUNT(s.id) AS suppliers_count,
|
||||
COUNT(r.request_txt) AS created_tz
|
||||
FROM requests_for_suppliers r
|
||||
LEFT JOIN suppliers s ON s.request_id = r.id
|
||||
WHERE r.user_id = $1
|
||||
`
|
||||
query := r.qb.Select(
|
||||
"COUNT(DISTINCT r.id) AS requests_count",
|
||||
"COUNT(s.id) AS suppliers_count",
|
||||
"COUNT(DISTINCT CASE WHEN r.request_txt IS NOT NULL THEN r.id END) AS created_tz",
|
||||
).From("requests_for_suppliers r").
|
||||
LeftJoin("suppliers s ON s.request_id = r.id").
|
||||
Where(sq.Eq{"r.user_id": userID})
|
||||
|
||||
err = r.pool.QueryRow(ctx, sqlQuery, userID).Scan(&requestsCount, &suppliersCount, &createdTZ)
|
||||
sqlQuery, args, err := query.ToSql()
|
||||
if err != nil {
|
||||
return 0, 0, 0, errs.NewInternalError(errs.DatabaseError, "failed to build query", err)
|
||||
}
|
||||
|
||||
err = r.pool.QueryRow(ctx, sqlQuery, args...).Scan(&requestsCount, &suppliersCount, &createdTZ)
|
||||
if err != nil {
|
||||
return 0, 0, 0, errs.NewInternalError(errs.DatabaseError, "failed to get statistics", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user