178 lines
4 KiB
Go
178 lines
4 KiB
Go
package merch
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"github.com/google/uuid"
|
|
"time"
|
|
)
|
|
|
|
type service struct {
|
|
repo repository
|
|
}
|
|
|
|
func newService(repo repository) *service {
|
|
return &service{
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (s *service) addMerch(payload MerchDTO, userUuid string) error {
|
|
merchUuid := uuid.NewString()
|
|
|
|
bundle := merchBundle{
|
|
&Merch{
|
|
CreatedAt: time.Time{},
|
|
UpdatedAt: sql.NullTime{Valid: false},
|
|
DeletedAt: sql.NullTime{Valid: false},
|
|
MerchUuid: merchUuid,
|
|
UserUuid: userUuid,
|
|
Name: payload.Name,
|
|
},
|
|
|
|
&Surugaya{
|
|
DeletedAt: sql.NullTime{},
|
|
MerchUuid: merchUuid,
|
|
Link: payload.OriginSurugaya.Link,
|
|
},
|
|
|
|
&Mandarake{
|
|
DeletedAt: sql.NullTime{},
|
|
MerchUuid: merchUuid,
|
|
Link: payload.OriginMandarake.Link,
|
|
},
|
|
}
|
|
return s.repo.addMerch(bundle)
|
|
}
|
|
|
|
func (s *service) getSingleMerch(userUuid, merchUuid string) (MerchDTO, error) {
|
|
bundle, err := s.repo.getSingleMerch(userUuid, merchUuid)
|
|
if err != nil {
|
|
return MerchDTO{}, err
|
|
}
|
|
|
|
return MerchDTO{
|
|
MerchUuid: bundle.Merch.MerchUuid,
|
|
Name: bundle.Merch.Name,
|
|
OriginSurugaya: SurugayaDTO{
|
|
Link: bundle.Surugaya.Link,
|
|
},
|
|
OriginMandarake: MandarakeDTO{
|
|
Link: bundle.Mandarake.Link,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (s *service) getAllMerch(userUuid string) ([]ListResponse, error) {
|
|
return s.repo.getAllMerch(userUuid)
|
|
}
|
|
|
|
func (s *service) updateMerch(payload UpdateMerchDTO, userUuid string) error {
|
|
if payload.MerchUuid == "" {
|
|
return errors.New("no merch uuid provided")
|
|
}
|
|
|
|
if payload.Origin == "" {
|
|
return errors.New("no origin provided")
|
|
}
|
|
|
|
return s.repo.updateMerch(payload, userUuid)
|
|
}
|
|
|
|
func (s *service) deleteMerch(userUuid, merchUuid string) error {
|
|
return s.repo.deleteMerch(userUuid, merchUuid)
|
|
}
|
|
|
|
func (s *service) getPrices(userUuid string, days string) ([]PricesResponse, error) {
|
|
merchList, err := s.repo.getAllUserMerch(userUuid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(merchList) == 0 {
|
|
return nil, errors.New("no merch found")
|
|
}
|
|
|
|
var response []PricesResponse
|
|
for _, item := range merchList {
|
|
response = append(response, PricesResponse{
|
|
MerchUuid: item.MerchUuid,
|
|
Name: item.Name,
|
|
Origins: []OriginWithPrices{},
|
|
})
|
|
}
|
|
|
|
pricesList, err := s.repo.getPricesWithDays(userUuid, getPeriod(days))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pricesMap := make(map[string]map[Origin][]PriceEntry)
|
|
for _, item := range pricesList {
|
|
if _, ok := pricesMap[item.MerchUuid]; !ok {
|
|
pricesMap[item.MerchUuid] = make(map[Origin][]PriceEntry)
|
|
}
|
|
|
|
pricesMap[item.MerchUuid][item.Origin] = append(pricesMap[item.MerchUuid][item.Origin], PriceEntry{
|
|
CreatedAt: item.CreatedAt.Unix(),
|
|
Value: item.Price,
|
|
})
|
|
}
|
|
|
|
for i := range response {
|
|
originSurugaya := OriginWithPrices{
|
|
Origin: surugaya,
|
|
Prices: pricesMap[response[i].MerchUuid][surugaya],
|
|
}
|
|
response[i].Origins = append(response[i].Origins, originSurugaya)
|
|
|
|
originMandarake := OriginWithPrices{
|
|
Origin: mandarake,
|
|
Prices: pricesMap[response[i].MerchUuid][mandarake],
|
|
}
|
|
response[i].Origins = append(response[i].Origins, originMandarake)
|
|
}
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func (s *service) getDistinctPrices(userUuid, merchUuid, days string) (PricesResponse, error) {
|
|
result, err := s.repo.getDistinctPrices(userUuid, merchUuid, getPeriod(days))
|
|
if err != nil {
|
|
return PricesResponse{}, err
|
|
}
|
|
|
|
if result == nil {
|
|
return PricesResponse{}, errors.New("no prices found")
|
|
}
|
|
|
|
originSurugaya := OriginWithPrices{
|
|
Origin: surugaya,
|
|
Prices: []PriceEntry{},
|
|
}
|
|
|
|
originMandarake := OriginWithPrices{
|
|
Origin: mandarake,
|
|
Prices: []PriceEntry{},
|
|
}
|
|
|
|
for _, item := range result {
|
|
switch item.Origin {
|
|
case surugaya:
|
|
originSurugaya.Prices = append(originSurugaya.Prices, PriceEntry{
|
|
CreatedAt: item.CreatedAt.Unix(),
|
|
Value: item.Price,
|
|
})
|
|
case mandarake:
|
|
originMandarake.Prices = append(originMandarake.Prices, PriceEntry{
|
|
CreatedAt: item.CreatedAt.Unix(),
|
|
Value: item.Price,
|
|
})
|
|
}
|
|
}
|
|
|
|
return PricesResponse{
|
|
MerchUuid: merchUuid,
|
|
Origins: []OriginWithPrices{originSurugaya, originMandarake},
|
|
}, nil
|
|
}
|