add validate errors in rule methods

This commit is contained in:
Pavel 2024-06-04 19:05:53 +03:00
parent e6ac0c4586
commit 558711eb04
9 changed files with 51 additions and 21 deletions

2
go.mod

@ -13,7 +13,7 @@ require (
github.com/twmb/franz-go v1.16.1
go.uber.org/zap v1.27.0
google.golang.org/protobuf v1.33.0
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240524125132-8d5d3e697390
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240604155436-38f4528609d7
penahub.gitlab.yandexcloud.net/backend/quiz/core.git v0.0.0-20240219174804-d78fd38511af
)

10
go.sum

@ -130,13 +130,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6 h1:oV+/HNX+JPoQ3/GUx08hio7d45WpY0AMGrFs7j70QlA=
penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240223054633-6cb3d5ce45b6/go.mod h1:lTmpjry+8evVkXWbEC+WMOELcFkRD1lFMc7J09mOndM=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240517190320-6bd772ee236c h1:XTkOH9osBDN08Q0n/Hkqp59h0FQ+N+68tW8Sxt8YWlU=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240517190320-6bd772ee236c/go.mod h1:oRyhT55ctjqp/7ZxIzkR7OsQ7T/NLibsfrbb7Ytns64=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240519122235-c74e1f395480 h1:lAHN8c6Vi4Fg+3zlSe1sdiyYYZuJDhisCS+iEsj6Huw=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240519122235-c74e1f395480/go.mod h1:oRyhT55ctjqp/7ZxIzkR7OsQ7T/NLibsfrbb7Ytns64=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240520145524-451212248881 h1:U1/WGQdwZsmrV/ta7Uqm13Dg07IPN/5omS8gzBJYZv4=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240520145524-451212248881/go.mod h1:oRyhT55ctjqp/7ZxIzkR7OsQ7T/NLibsfrbb7Ytns64=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240524125132-8d5d3e697390 h1:OZGApmGFgv+AMilPPPNr+Qo/aOTXGM6p3NMU34m2RCs=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240524125132-8d5d3e697390/go.mod h1:oRyhT55ctjqp/7ZxIzkR7OsQ7T/NLibsfrbb7Ytns64=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240604155436-38f4528609d7 h1:rwocr+GsnkSYV/lif9Li+yc7rEqlciEBGkM2wc8lsNw=
penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240604155436-38f4528609d7/go.mod h1:G1ZAWaQq6WW1wG9Shy57K4ZIezuhaBckQgsqQ+lhe94=
penahub.gitlab.yandexcloud.net/backend/quiz/core.git v0.0.0-20240219174804-d78fd38511af h1:jQ7HaXSutDX5iepU7VRImxhikK7lV/lBKkiloOZ4Emo=
penahub.gitlab.yandexcloud.net/backend/quiz/core.git v0.0.0-20240219174804-d78fd38511af/go.mod h1:5S5YwjSXWmnEKjBjG6MtyGtFmljjukDRS8CwHk/CF/I=

@ -1,6 +1,8 @@
package controllers
import (
"amocrm/internal/service_errors"
"errors"
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/lib/pq"
@ -25,7 +27,7 @@ func (c *Controller) ChangeQuizSettings(ctx *fiber.Ctx) error {
return ctx.Status(fiber.StatusBadRequest).SendString("failed convert quizID to int")
}
//accountID := "654a8909725f47e926f0bebc"
//accountID := "64f2cd7a7047f28fdabf6d9e"
var request model.RulesReq
if err := ctx.BodyParser(&request); err != nil {
@ -33,9 +35,13 @@ func (c *Controller) ChangeQuizSettings(ctx *fiber.Ctx) error {
}
err = c.service.ChangeQuizSettings(ctx.Context(), &request, accountID, quizIDInt)
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
switch {
case errors.Is(err, service_errors.ErrNotFound):
return ctx.Status(fiber.StatusNotFound).SendString("rule not found")
default:
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
}
return ctx.SendStatus(fiber.StatusOK)
}
@ -56,7 +62,7 @@ func (c *Controller) SetQuizSettings(ctx *fiber.Ctx) error {
return ctx.Status(fiber.StatusBadRequest).SendString("failed convert quizID to int")
}
//accountID := "654a8909725f47e926f0bebc"
//accountID := "64f2cd7a7047f28fdabf6d9e"
var request model.RulesReq
if err := ctx.BodyParser(&request); err != nil {
@ -69,7 +75,12 @@ func (c *Controller) SetQuizSettings(ctx *fiber.Ctx) error {
if ok && pqErr.Code == "23505" {
return ctx.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("quiz settings already exist for accountID %s and quizID %d", accountID, quizIDInt))
}
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
switch {
case errors.Is(err, service_errors.ErrNotFound):
return ctx.Status(fiber.StatusNotFound).SendString("not found")
default:
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
}
return ctx.SendStatus(fiber.StatusOK)
}
@ -88,7 +99,12 @@ func (c *Controller) GettingQuizRules(ctx *fiber.Ctx) error {
response, err := c.service.GettingQuizRules(ctx.Context(), quizIDInt)
if err != nil {
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
switch {
case errors.Is(err, service_errors.ErrNotFound):
return ctx.Status(fiber.StatusNotFound).SendString("rule not found")
default:
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
}
return ctx.Status(fiber.StatusOK).JSON(response)
}

@ -65,11 +65,12 @@ func (c *Controller) GetCurrentAccount(ctx *fiber.Ctx) error {
response, err := c.service.GetCurrentAccount(ctx.Context(), accountID)
if err != nil {
if errors.Is(err, service_errors.ErrUserNotFound) {
switch {
case errors.Is(err, service_errors.ErrNotFound):
return ctx.Status(fiber.StatusNotFound).SendString("user not found")
default:
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return ctx.Status(fiber.StatusOK).JSON(response)
}

@ -8,7 +8,7 @@ import (
type Config struct {
AppName string `env:"APP_NAME" envDefault:"amocrm"`
HTTPHost string `env:"HTTP_HOST" envDefault:"10.8.0.18"`
HTTPHost string `env:"HTTP_HOST" envDefault:"localhost"`
HTTPPort string `env:"HTTP_PORT" envDefault:"8001"`
PostgresCredentials string `env:"PG_CRED" envDefault:"host=localhost port=35432 user=squiz password=Redalert2 dbname=squiz sslmode=disable"`
KafkaBrokers string `env:"KAFKA_BROKERS" envDefault:"localhost:9092"`

@ -2,7 +2,9 @@ package service
import (
"amocrm/internal/models"
"amocrm/internal/service_errors"
"context"
"database/sql"
"go.uber.org/zap"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
)
@ -10,6 +12,9 @@ import (
func (s *Service) ChangeQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error {
err := s.repository.AmoRepo.ChangeQuizSettings(ctx, request, accountID, quizID)
if err != nil {
if err == sql.ErrNoRows {
return service_errors.ErrNotFound
}
s.logger.Error("error change quiz settings", zap.Error(err))
return err
}
@ -40,6 +45,9 @@ func (s *Service) ChangeQuizSettings(ctx context.Context, request *model.RulesRe
func (s *Service) SetQuizSettings(ctx context.Context, request *model.RulesReq, accountID string, quizID int) error {
err := s.repository.AmoRepo.SetQuizSettings(ctx, request, accountID, quizID)
if err != nil {
if err == sql.ErrNoRows {
return service_errors.ErrNotFound
}
s.logger.Error("error setting quiz settings", zap.Error(err))
return err
}
@ -70,6 +78,9 @@ func (s *Service) SetQuizSettings(ctx context.Context, request *model.RulesReq,
func (s *Service) GettingQuizRules(ctx context.Context, quizID int) (*model.Rule, error) {
rule, err := s.repository.AmoRepo.GettingQuizRules(ctx, quizID)
if err != nil {
if err == sql.ErrNoRows {
return nil, service_errors.ErrNotFound
}
s.logger.Error("error getting quiz settings", zap.Error(err))
return nil, err
}

@ -46,7 +46,7 @@ func (s *Service) GetCurrentAccount(ctx context.Context, accountID string) (*mod
user, err := s.repository.AmoRepo.GetCurrentAccount(ctx, accountID)
if err != nil {
if err == sql.ErrNoRows {
return nil, service_errors.ErrUserNotFound
return nil, service_errors.ErrNotFound
}
s.logger.Error("error getting current account in getCurrentAccount service", zap.Error(err))
return nil, err

@ -2,4 +2,4 @@ package service_errors
import "errors"
var ErrUserNotFound = errors.New("user not found")
var ErrNotFound = errors.New("not found")

@ -52,6 +52,8 @@ paths:
$ref: '#/components/schemas/User'
'401':
$ref: '#/components/responses/401'
'404':
$ref: '#/components/responses/404'
'500':
$ref: '#/components/responses/500'
/webhook/create:
@ -448,6 +450,8 @@ paths:
$ref: "#/components/schemas/Rule"
'400':
$ref: '#/components/responses/400'
'404':
$ref: '#/components/responses/404'
'500':
$ref: '#/components/responses/500'
post:
@ -474,6 +478,8 @@ paths:
$ref: '#/components/responses/400'
'401':
$ref: '#/components/responses/401'
'404':
$ref: '#/components/responses/404'
'500':
$ref: '#/components/responses/500'
patch:
@ -500,6 +506,8 @@ paths:
$ref: '#/components/responses/400'
'401':
$ref: '#/components/responses/401'
'404':
$ref: '#/components/responses/404'
'500':
$ref: '#/components/responses/500'