-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
…nto deploy
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
contextutils "github.com/go-park-mail-ru/2023_2_Hamster/internal/common/context_utils" | ||
response "github.com/go-park-mail-ru/2023_2_Hamster/internal/common/http" | ||
"github.com/go-park-mail-ru/2023_2_Hamster/internal/models" | ||
|
||
"github.com/go-park-mail-ru/2023_2_Hamster/internal/common/logger" | ||
"github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/goal" | ||
) | ||
|
||
type Handler struct { | ||
goalUsecase goal.Useace | ||
log logger.Logger | ||
} | ||
|
||
func NewHandler(gu goal.Useace, l logger.Logger) *Handler { | ||
return &Handler{ | ||
goalUsecase: gu, | ||
log: l, | ||
} | ||
} | ||
|
||
func (h *Handler) CreateGoal(w http.ResponseWriter, r *http.Request) { | ||
// get user from context | ||
user, err := response.GetUserFromRequest(r) | ||
if err != nil { | ||
response.ErrorResponse(w, http.StatusUnauthorized, err, response.ErrUnauthorized.Error(), h.log) | ||
return | ||
} | ||
|
||
// unmarshal request body | ||
var goalInput goal.GoalCreateRequest | ||
|
||
decoder := json.NewDecoder(r.Body) | ||
if err := decoder.Decode(&goalInput); err != nil { | ||
h.log.WithField( | ||
"Request-Id", contextutils.GetReqID(r.Context()), | ||
).Errorf("[handler] Error Corupted request body: %v", err) | ||
response.ErrorResponse(w, http.StatusBadRequest, err, "Corrupted request body can't unmarshal", h.log) | ||
return | ||
} | ||
defer r.Body.Close() | ||
|
||
goalInput.UserId = user.ID | ||
|
||
goalId, err := h.goalUsecase.CreateGoal(r.Context(), goalInput) | ||
if err != nil { | ||
response.ErrorResponse(w, http.StatusBadRequest, err, "can't create goal", h.log) | ||
return | ||
} | ||
|
||
response.SuccessResponse(w, http.StatusOK, goalId) | ||
} | ||
|
||
func (h *Handler) UpdateGoal(w http.ResponseWriter, r *http.Request) { | ||
user, err := response.GetUserFromRequest(r) | ||
if err != nil { | ||
response.ErrorResponse(w, http.StatusUnauthorized, err, response.ErrUnauthorized.Error(), h.log) | ||
return | ||
} | ||
|
||
var goalInput models.Goal | ||
|
||
decoder := json.NewDecoder(r.Body) | ||
if err := decoder.Decode(&goalInput); err != nil { | ||
h.log.WithField( | ||
"Request-Id", contextutils.GetReqID(r.Context()), | ||
).Errorf("[handler] Error Corupted request body: %v", err) | ||
response.ErrorResponse(w, http.StatusBadRequest, err, "Corrupted request body can't unmarshal", h.log) | ||
return | ||
} | ||
defer r.Body.Close() | ||
|
||
goalInput.UserId = user.ID | ||
|
||
if err := h.goalUsecase.UpdateGoal(r.Context(), &goalInput); err != nil { | ||
response.ErrorResponse(w, http.StatusBadRequest, err, "can't update goal", h.log) | ||
return | ||
} | ||
|
||
response.SuccessResponse(w, http.StatusOK, response.NilBody{}) | ||
} | ||
|
||
func (h *Handler) DeleteGoal(w http.ResponseWriter, r *http.Request) { | ||
user, err := response.GetUserFromRequest(r) | ||
if err != nil { | ||
response.ErrorResponse(w, http.StatusUnauthorized, err, response.ErrUnauthorized.Error(), h.log) | ||
return | ||
} | ||
|
||
var goalInput goal.GoalDeleteRequest | ||
|
||
decoder := json.NewDecoder(r.Body) | ||
if err := decoder.Decode(&goalInput); err != nil { | ||
h.log.WithField( | ||
"Request-Id", contextutils.GetReqID(r.Context()), | ||
).Errorf("[handler] Error Corupted request body: %v", err) | ||
response.ErrorResponse(w, http.StatusBadRequest, err, "Corrupted request body can't unmarshal", h.log) | ||
return | ||
} | ||
defer r.Body.Close() | ||
|
||
if err := h.goalUsecase.DeleteGoal(r.Context(), goalInput.ID, user.ID); err != nil { | ||
response.ErrorResponse(w, http.StatusBadRequest, err, "can't delete goal", h.log) | ||
return | ||
} | ||
|
||
response.SuccessResponse(w, http.StatusOK, response.NilBody{}) | ||
} | ||
|
||
func (h *Handler) GetGoals(w http.ResponseWriter, r *http.Request) { | ||
user, err := response.GetUserFromRequest(r) | ||
if err != nil { | ||
response.ErrorResponse(w, http.StatusUnauthorized, err, response.ErrUnauthorized.Error(), h.log) | ||
return | ||
} | ||
|
||
goals, err := h.goalUsecase.GetGoals(r.Context(), user.ID) | ||
if err != nil { | ||
response.ErrorResponse(w, http.StatusInternalServerError, err, "can't get goals", h.log) | ||
return | ||
} | ||
|
||
response.SuccessResponse(w, http.StatusOK, goals) | ||
} | ||
|
||
func (h *Handler) CheckGoalsState(w http.ResponseWriter, r *http.Request) { | ||
user, err := response.GetUserFromRequest(r) | ||
if err != nil { | ||
response.ErrorResponse(w, http.StatusUnauthorized, err, response.ErrUnauthorized.Error(), h.log) | ||
return | ||
} | ||
|
||
goals, err := h.goalUsecase.CheckGoalsState(r.Context(), user.ID) | ||
if err != nil { | ||
response.ErrorResponse(w, http.StatusBadRequest, err, "can't check goals state", h.log) | ||
return | ||
} | ||
|
||
response.SuccessResponse(w, http.StatusOK, goals) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package goal | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-park-mail-ru/2023_2_Hamster/internal/models" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type Useace interface { | ||
CreateGoal(ctx context.Context, goal GoalCreateRequest) (uuid.UUID, error) | ||
UpdateGoal(ctx context.Context, goal *models.Goal) error | ||
DeleteGoal(ctx context.Context, goalId uuid.UUID, userId uuid.UUID) error | ||
GetGoals(ctx context.Context, userId uuid.UUID) ([]models.Goal, error) | ||
|
||
CheckGoalsState(ctx context.Context, userId uuid.UUID) ([]models.Goal, error) // check if any goals are completed | ||
} | ||
|
||
type Repository interface { | ||
CreateGoal(ctx context.Context, goal models.Goal) (uuid.UUID, error) | ||
UpdateGoal(ctx context.Context, goal *models.Goal) error | ||
DeleteGoal(ctx context.Context, goalId uuid.UUID) error | ||
GetGoals(ctx context.Context, userId uuid.UUID) ([]models.Goal, error) | ||
|
||
CheckGoalsState(ctx context.Context, userId uuid.UUID) ([]models.Goal, error) // check if any goals are completed | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package goal | ||
|
||
import "github.com/google/uuid" | ||
|
||
type ( | ||
GoalCreateRequest struct { | ||
UserId uuid.UUID `json:"user_id" valid:"required"` | ||
Name string `json:"name" valid:"required"` | ||
Description string `json:"description" valid:"-"` | ||
Total float64 `json:"total" valid:"required,greaterzero"` | ||
Date string `json:"date" valid:"isdate"` | ||
} | ||
|
||
GoalUpdateRequest struct { | ||
ID uuid.UUID `json:"id" valid:"required"` | ||
UserId uuid.UUID `json:"user_id" valid:"required"` | ||
Name string `json:"name" valid:"required"` | ||
Description string `json:"description" valid:"-"` | ||
Total float64 `json:"total" valid:"required,greaterzero"` | ||
Date string `json:"date" valid:"isdate"` | ||
} | ||
|
||
GoalDeleteRequest struct { | ||
ID uuid.UUID `json:"id" valid:"required"` | ||
} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/go-park-mail-ru/2023_2_Hamster/cmd/api/init/db/postgresql" | ||
"github.com/go-park-mail-ru/2023_2_Hamster/internal/common/logger" | ||
"github.com/go-park-mail-ru/2023_2_Hamster/internal/models" | ||
"github.com/google/uuid" | ||
) | ||
|
||
const ( | ||
GoalGet = `SELECT user_id, "name", "description", target, "date" FROM goal WHERE id=$1;` | ||
|
||
GoalCreate = `INSERT INTO goal (user_id, "name", "description", target, "date") | ||
VALUES ($1, $2, $3, $4, $5) | ||
RETURNING id;` | ||
|
||
GoalUpdate = `UPDATE goal SET "name"=$1, "description"=$2, target=$3, "date"=$4 WHERE id=$5;` | ||
|
||
GoalDelete = "DELETE FROM goal WHERE id = $1;" | ||
|
||
GoalAll = `SELECT * FROM goal WHERE user_id = $1;` | ||
) | ||
|
||
type Repository struct { | ||
db postgresql.DbConn | ||
log logger.Logger | ||
} | ||
|
||
func NewRepository(db postgresql.DbConn, log logger.Logger) *Repository { | ||
return &Repository{ | ||
db: db, | ||
log: log, | ||
} | ||
} | ||
|
||
func (r *Repository) CreateGoal(ctx context.Context, goal models.Goal) (uuid.UUID, error) { | ||
row := r.db.QueryRow(ctx, GoalCreate, | ||
goal.UserId, | ||
goal.Name, | ||
goal.Description, | ||
goal.Target, | ||
goal.Date, | ||
) | ||
|
||
var id uuid.UUID | ||
if err := row.Scan(&id); err != nil { | ||
return uuid.Nil, err | ||
} | ||
|
||
return id, nil | ||
} | ||
|
||
func (r *Repository) UpdateGoal(ctx context.Context, goal *models.Goal) error { | ||
_, err := r.db.Exec(ctx, GoalUpdate, | ||
goal.Name, | ||
goal.Description, | ||
goal.Target, | ||
goal.Date, | ||
goal.ID, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("[repo] UpdateGoal: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *Repository) DeleteGoal(ctx context.Context, goalId uuid.UUID) error { | ||
|
||
} | ||
Check failure on line 73 in internal/microservices/goal/repository/postgres/repository.go GitHub Actions / test
Check failure on line 73 in internal/microservices/goal/repository/postgres/repository.go GitHub Actions / lint_and_test
|
||
|
||
func (r *Repository) GetGoals(ctx context.Context, userId uuid.UUID) ([]models.Goal, error) { | ||
|
||
} | ||
Check failure on line 77 in internal/microservices/goal/repository/postgres/repository.go GitHub Actions / test
Check failure on line 77 in internal/microservices/goal/repository/postgres/repository.go GitHub Actions / lint_and_test
|
||
|
||
func (r *Repository) CheckGoalsState(ctx context.Context, userId uuid.UUID) ([]models.Goal, error) { | ||
|
||
} | ||
Check failure on line 81 in internal/microservices/goal/repository/postgres/repository.go GitHub Actions / test
Check failure on line 81 in internal/microservices/goal/repository/postgres/repository.go GitHub Actions / lint_and_test
|