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 (
"net/http"
"strings"
"time"
"github.com/hay-kot/homebox/backend/internal/repo"
@ -54,13 +53,12 @@ func (ctrl *V1Controller) handleGroupGeneral() server.HandlerFunc {
switch r.Method {
case http.MethodGet:
group, err := ctrl.svc.Group.Get(ctx)
group, err := ctrl.repo.Groups.GroupByID(ctx, ctx.GID)
if err != nil {
log.Err(err).Msg("failed to get group")
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)
case http.MethodPut:
@ -74,7 +72,7 @@ func (ctrl *V1Controller) handleGroupGeneral() server.HandlerFunc {
log.Err(err).Msg("failed to update group")
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)
}

View file

@ -2,6 +2,7 @@ package repo
import (
"context"
"strings"
"time"
"github.com/google/uuid"
@ -52,7 +53,7 @@ func mapToGroup(g *ent.Group) Group {
Name: g.Name,
CreatedAt: g.CreatedAt,
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) {
currency := group.Currency(data.Currency)
currency := group.Currency(strings.ToLower(data.Currency))
entity, err := r.db.Group.UpdateOneID(ID).
SetName(data.Name).

View file

@ -2,7 +2,6 @@ package services
import (
"errors"
"strings"
"time"
"github.com/hay-kot/homebox/backend/internal/repo"
@ -13,10 +12,6 @@ type GroupService struct {
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) {
if data.Name == "" {
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")
}
data.Currency = strings.ToLower(data.Currency)
return svc.repos.Groups.GroupUpdate(ctx.Context, ctx.GID, data)
}