cleanup more duplicate service code

This commit is contained in:
Hayden 2022-10-29 19:26:11 -08:00
parent 09d0222ed1
commit cc255b9690
3 changed files with 5 additions and 13 deletions

View file

@ -2,7 +2,6 @@ package v1
import ( import (
"net/http" "net/http"
"strings"
"time" "time"
"github.com/hay-kot/homebox/backend/internal/repo" "github.com/hay-kot/homebox/backend/internal/repo"
@ -54,13 +53,12 @@ func (ctrl *V1Controller) handleGroupGeneral() server.HandlerFunc {
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
group, err := ctrl.svc.Group.Get(ctx) group, err := ctrl.repo.Groups.GroupByID(ctx, ctx.GID)
if err != nil { if err != nil {
log.Err(err).Msg("failed to get group") log.Err(err).Msg("failed to get group")
return validate.NewRequestError(err, http.StatusInternalServerError) return validate.NewRequestError(err, http.StatusInternalServerError)
} }
group.Currency = strings.ToUpper(group.Currency) // TODO: Hack to fix the currency enums being lower caseÍ
return server.Respond(w, http.StatusOK, group) return server.Respond(w, http.StatusOK, group)
case http.MethodPut: case http.MethodPut:
@ -74,7 +72,7 @@ func (ctrl *V1Controller) handleGroupGeneral() server.HandlerFunc {
log.Err(err).Msg("failed to update group") log.Err(err).Msg("failed to update group")
return validate.NewRequestError(err, http.StatusInternalServerError) return validate.NewRequestError(err, http.StatusInternalServerError)
} }
group.Currency = strings.ToUpper(group.Currency) // TODO: Hack to fix the currency enums being lower case
return server.Respond(w, http.StatusOK, group) return server.Respond(w, http.StatusOK, group)
} }

View file

@ -2,6 +2,7 @@ package repo
import ( import (
"context" "context"
"strings"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@ -52,7 +53,7 @@ func mapToGroup(g *ent.Group) Group {
Name: g.Name, Name: g.Name,
CreatedAt: g.CreatedAt, CreatedAt: g.CreatedAt,
UpdatedAt: g.UpdatedAt, UpdatedAt: g.UpdatedAt,
Currency: g.Currency.String(), Currency: strings.ToUpper(g.Currency.String()),
} }
} }
@ -76,7 +77,7 @@ func (r *GroupRepository) GroupCreate(ctx context.Context, name string) (Group,
} }
func (r *GroupRepository) GroupUpdate(ctx context.Context, ID uuid.UUID, data GroupUpdate) (Group, error) { func (r *GroupRepository) GroupUpdate(ctx context.Context, ID uuid.UUID, data GroupUpdate) (Group, error) {
currency := group.Currency(data.Currency) currency := group.Currency(strings.ToLower(data.Currency))
entity, err := r.db.Group.UpdateOneID(ID). entity, err := r.db.Group.UpdateOneID(ID).
SetName(data.Name). SetName(data.Name).

View file

@ -2,7 +2,6 @@ package services
import ( import (
"errors" "errors"
"strings"
"time" "time"
"github.com/hay-kot/homebox/backend/internal/repo" "github.com/hay-kot/homebox/backend/internal/repo"
@ -13,10 +12,6 @@ type GroupService struct {
repos *repo.AllRepos repos *repo.AllRepos
} }
func (svc *GroupService) Get(ctx Context) (repo.Group, error) {
return svc.repos.Groups.GroupByID(ctx.Context, ctx.GID)
}
func (svc *GroupService) UpdateGroup(ctx Context, data repo.GroupUpdate) (repo.Group, error) { func (svc *GroupService) UpdateGroup(ctx Context, data repo.GroupUpdate) (repo.Group, error) {
if data.Name == "" { if data.Name == "" {
data.Name = ctx.User.GroupName data.Name = ctx.User.GroupName
@ -26,8 +21,6 @@ func (svc *GroupService) UpdateGroup(ctx Context, data repo.GroupUpdate) (repo.G
return repo.Group{}, errors.New("currency cannot be empty") return repo.Group{}, errors.New("currency cannot be empty")
} }
data.Currency = strings.ToLower(data.Currency)
return svc.repos.Groups.GroupUpdate(ctx.Context, ctx.GID, data) return svc.repos.Groups.GroupUpdate(ctx.Context, ctx.GID, data)
} }