generated from PenaSide/GolangTemplate
update issue 3
This commit is contained in:
parent
298e079f0f
commit
c149d6a743
@ -16,6 +16,7 @@ import (
|
|||||||
|
|
||||||
type historyService interface {
|
type historyService interface {
|
||||||
GetHistoryList(context.Context, *dto.GetHistories) (*models.PaginationResponse[models.History], errors.Error)
|
GetHistoryList(context.Context, *dto.GetHistories) (*models.PaginationResponse[models.History], errors.Error)
|
||||||
|
GetRecentTariffs(context.Context, string) ([]string, errors.Error) //new
|
||||||
}
|
}
|
||||||
|
|
||||||
type Deps struct {
|
type Deps struct {
|
||||||
@ -69,3 +70,26 @@ func (receiver *Controller) GetHistoryList(ctx echo.Context, params swagger.GetH
|
|||||||
|
|
||||||
return ctx.JSON(http.StatusOK, histories)
|
return ctx.JSON(http.StatusOK, histories)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO:tests
|
||||||
|
func (receiver *Controller) GetRecentTariffs(ctx echo.Context) error {
|
||||||
|
userID, ok := ctx.Get(models.AuthJWTDecodedUserIDKey).(string)
|
||||||
|
if !ok {
|
||||||
|
receiver.logger.Error("failed to convert jwt payload to string on <GetRecentTariffs> of <HistoryController>")
|
||||||
|
return errors.HTTP(ctx, errors.New(
|
||||||
|
fmt.Errorf("failed to convert jwt payload to string: %s", userID),
|
||||||
|
errors.ErrInvalidArgs,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
tariffs, err := receiver.historyService.GetRecentTariffs(ctx.Request().Context(), userID)
|
||||||
|
if err != nil {
|
||||||
|
receiver.logger.Error("failed to get recent tariffs on <GetRecentTariffs> of <HistoryController>",
|
||||||
|
zap.String("userId", userID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
return errors.HTTP(ctx, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.JSON(http.StatusOK, tariffs)
|
||||||
|
}
|
||||||
|
@ -3,7 +3,9 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"log"
|
"log"
|
||||||
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/fields"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
@ -107,3 +109,51 @@ func (receiver *HistoryRepository) CountAll(ctx context.Context, dto *dto.GetHis
|
|||||||
|
|
||||||
return count, nil
|
return count, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO:tests
|
||||||
|
// GetRecentTariffs method for processing a user request with data aggregation with a limit of 100 sorted in descending order
|
||||||
|
func (receiver *HistoryRepository) GetRecentTariffs(ctx context.Context, userID string) ([]string, errors.Error) {
|
||||||
|
var result []struct {
|
||||||
|
ID string `bson:"tariffID"`
|
||||||
|
}
|
||||||
|
filter := bson.D{
|
||||||
|
{fields.History.UserID, userID},
|
||||||
|
{fields.History.IsDeleted, false},
|
||||||
|
}
|
||||||
|
pipeline := mongo.Pipeline{
|
||||||
|
{{"$match", filter}},
|
||||||
|
{{"$unwind", "$rawDetails"}},
|
||||||
|
{{"$match", bson.D{{"rawDetails.Key", "tariffs"}}}},
|
||||||
|
{{"$unwind", "$rawDetails.Value"}},
|
||||||
|
{{"$group", bson.D{{"_id", "$rawDetails.Value.Key.id"}}}},
|
||||||
|
{{"$sort", bson.D{{"createdAt", -1}}}}, // -1 specifies descending order
|
||||||
|
{{"$limit", 100}},
|
||||||
|
}
|
||||||
|
cursor, err := receiver.mongoDB.Aggregate(ctx, pipeline)
|
||||||
|
if err != nil {
|
||||||
|
receiver.logger.Error("failed to get recent tariffs on <GetRecentTariffs> of <HistoryRepository>",
|
||||||
|
zap.String("userId", userID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
return nil, errors.New(
|
||||||
|
fmt.Errorf("failed to get recent tariffs on <GetRecentTariffs> of <HistoryRepository>: %w", err),
|
||||||
|
errors.ErrInternalError,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cursor.All(ctx, &result); err != nil {
|
||||||
|
receiver.logger.Error("failed to decode recent tariffs on <GetRecentTariffs> of <HistoryRepository>",
|
||||||
|
zap.String("userId", userID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
return nil, errors.New(
|
||||||
|
fmt.Errorf("failed to decode recent tariffs on <GetRecentTariffs> of <HistoryRepository>: %w", err),
|
||||||
|
errors.ErrInternalError,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
tariffs := make([]string, len(result))
|
||||||
|
for i, item := range result {
|
||||||
|
tariffs[i] = item.ID
|
||||||
|
}
|
||||||
|
return tariffs, nil
|
||||||
|
}
|
||||||
|
@ -35,6 +35,7 @@ type walletController interface {
|
|||||||
|
|
||||||
type historyController interface {
|
type historyController interface {
|
||||||
GetHistoryList(ctx echo.Context, params GetHistoryParams) error
|
GetHistoryList(ctx echo.Context, params GetHistoryParams) error
|
||||||
|
GetRecentTariffs(ctx echo.Context) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Deps struct {
|
type Deps struct {
|
||||||
@ -147,6 +148,10 @@ func (receiver *API) GetHistory(ctx echo.Context, params GetHistoryParams) error
|
|||||||
return receiver.historyController.GetHistoryList(ctx, params)
|
return receiver.historyController.GetHistoryList(ctx, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (receiver *API) GetRecentTariffs(ctx echo.Context) error {
|
||||||
|
return receiver.historyController.GetRecentTariffs(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
// Wallet
|
// Wallet
|
||||||
|
|
||||||
func (receiver *API) RequestMoney(ctx echo.Context) error {
|
func (receiver *API) RequestMoney(ctx echo.Context) error {
|
||||||
|
@ -16,6 +16,7 @@ type historyRepository interface {
|
|||||||
CountAll(context.Context, *dto.GetHistories) (int64, errors.Error)
|
CountAll(context.Context, *dto.GetHistories) (int64, errors.Error)
|
||||||
FindMany(context.Context, *dto.GetHistories) ([]models.History, errors.Error)
|
FindMany(context.Context, *dto.GetHistories) ([]models.History, errors.Error)
|
||||||
Insert(context.Context, *models.History) (*models.History, errors.Error)
|
Insert(context.Context, *models.History) (*models.History, errors.Error)
|
||||||
|
GetRecentTariffs(context.Context, string) ([]string, errors.Error) //new
|
||||||
}
|
}
|
||||||
|
|
||||||
type Deps struct {
|
type Deps struct {
|
||||||
@ -90,3 +91,36 @@ func (receiver *Service) CreateHistory(ctx context.Context, history *models.Hist
|
|||||||
|
|
||||||
return createdHistory, nil
|
return createdHistory, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO:tests
|
||||||
|
func (receiver *Service) GetRecentTariffs(ctx context.Context, userID string) ([]string, errors.Error) {
|
||||||
|
// Validation of input data
|
||||||
|
if userID == "" {
|
||||||
|
receiver.logger.Error("user id is missing in <GetRecentTariffs> of <HistoryService>")
|
||||||
|
return nil, errors.New(
|
||||||
|
fmt.Errorf("user id is missing: %w", errors.ErrInvalidArgs),
|
||||||
|
errors.ErrInvalidArgs,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
tariffs, err := receiver.repository.GetRecentTariffs(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
receiver.logger.Error(
|
||||||
|
"failed to get recent tariffs in <GetRecentTariffs> of <HistoryService>",
|
||||||
|
zap.String("userId", userID),
|
||||||
|
zap.Error(err),
|
||||||
|
)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Processing the received response, optional
|
||||||
|
if len(tariffs) == 0 {
|
||||||
|
receiver.logger.Info(
|
||||||
|
"no recent tariffs found for user in <GetRecentTariffs> of <HistoryService>",
|
||||||
|
zap.String("userId", userID),
|
||||||
|
)
|
||||||
|
// если слайс тарифов пуст является ли это ошибкой, возвращать ошибку или пустой слайс?
|
||||||
|
}
|
||||||
|
|
||||||
|
return tariffs, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user