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,12 +153,11 @@ 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) {
|
func (r *requestRepository) GetDetailByID(ctx context.Context, id uuid.UUID) (*model.RequestDetail, error) {
|
||||||
sqlQuery := `
|
query := r.qb.Select(
|
||||||
SELECT
|
"r.id AS request_id",
|
||||||
r.id AS request_id,
|
"r.request_txt AS title",
|
||||||
r.request_txt AS title,
|
"r.final_update_tz AS mail_text",
|
||||||
r.final_update_tz AS mail_text,
|
`COALESCE(json_agg(
|
||||||
COALESCE(json_agg(
|
|
||||||
json_build_object(
|
json_build_object(
|
||||||
'email', COALESCE(s.email, ''),
|
'email', COALESCE(s.email, ''),
|
||||||
'phone', COALESCE(s.phone, ''),
|
'phone', COALESCE(s.phone, ''),
|
||||||
@@ -166,17 +165,21 @@ func (r *requestRepository) GetDetailByID(ctx context.Context, id uuid.UUID) (*m
|
|||||||
'company_id', s.id,
|
'company_id', s.id,
|
||||||
'url', COALESCE(s.url, '')
|
'url', COALESCE(s.url, '')
|
||||||
)
|
)
|
||||||
) FILTER (WHERE s.id IS NOT NULL), '[]') AS suppliers
|
) FILTER (WHERE s.id IS NOT NULL), '[]') AS suppliers`,
|
||||||
FROM requests_for_suppliers r
|
).From("requests_for_suppliers r").
|
||||||
LEFT JOIN suppliers s ON s.request_id = r.id
|
LeftJoin("suppliers s ON s.request_id = r.id").
|
||||||
WHERE r.id = $1
|
Where(sq.Eq{"r.id": id}).
|
||||||
GROUP BY r.id, r.request_txt, r.final_update_tz
|
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{}
|
detail := &model.RequestDetail{}
|
||||||
var suppliersJSON []byte
|
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,
|
&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) {
|
func (r *requestRepository) GetUserStatistics(ctx context.Context, userID int) (requestsCount, suppliersCount, createdTZ int, err error) {
|
||||||
sqlQuery := `
|
query := r.qb.Select(
|
||||||
SELECT
|
"COUNT(DISTINCT r.id) AS requests_count",
|
||||||
COUNT(DISTINCT r.id) AS requests_count,
|
"COUNT(s.id) AS suppliers_count",
|
||||||
COUNT(s.id) AS suppliers_count,
|
"COUNT(DISTINCT CASE WHEN r.request_txt IS NOT NULL THEN r.id END) AS created_tz",
|
||||||
COUNT(r.request_txt) AS created_tz
|
).From("requests_for_suppliers r").
|
||||||
FROM requests_for_suppliers r
|
LeftJoin("suppliers s ON s.request_id = r.id").
|
||||||
LEFT JOIN suppliers s ON s.request_id = r.id
|
Where(sq.Eq{"r.user_id": userID})
|
||||||
WHERE r.user_id = $1
|
|
||||||
`
|
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
return 0, 0, 0, errs.NewInternalError(errs.DatabaseError, "failed to get statistics", err)
|
return 0, 0, 0, errs.NewInternalError(errs.DatabaseError, "failed to get statistics", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user