2024-10-24 22:32:57 +02:00

154 lines
4.4 KiB
Go

// MIT License
//
// Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package repo
import (
"context"
"database/sql"
"encoding/json"
"time"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/repo/sqlc"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/repo"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
)
type eventRepository struct {
queries sqlc.Querier
}
func NewEventRepository(db *sql.DB) repo.EventRepository {
return &eventRepository{
queries: sqlc.New(db),
}
}
// Create implements repo.EventRepository.
func (e *eventRepository) Create(
ctx context.Context,
evEntity *model.EventEntity,
) (*model.EventEntity, error) {
panic("unimplemented")
}
func convToEventRetrieved(eventDTO *sqlc.GetEventByIDRow) (*model.EventRetrieved, error) {
// marshal owner and users
var owner model.UserBaseRetrieved
err := json.Unmarshal(eventDTO.Owner, &owner)
if err != nil {
// Unexpected
log.ErrorLog("json unmarshal error", "err", err)
return nil, err
}
var users []model.UserBaseRetrieved
err = json.Unmarshal(eventDTO.Users, &users)
if err != nil {
// Unexpected
log.ErrorLog("json unmarshal error", "err", err)
return nil, err
}
eventRetrieved := &model.EventRetrieved{
ID: int(eventDTO.ID),
Name: eventDTO.Name,
Description: eventDTO.Description.String,
TotalAmount: model.MakeMoney(
int(eventDTO.TotalAmount.Int32),
model.Currency(eventDTO.DefaultCurrency),
),
DefaultCurrency: model.Currency(eventDTO.DefaultCurrency),
CreatedAt: eventDTO.CreatedAt,
UpdatedAt: eventDTO.UpdatedAt,
Owner: &owner,
Users: users,
}
return eventRetrieved, nil
}
// GetByID implements repo.EventRepository.
func (e *eventRepository) GetByID(ctx context.Context, eventID int) (*model.EventRetrieved, error) {
eventDTO, err := e.queries.GetEventByID(ctx, int32(eventID))
if err != nil {
log.ErrorLog("query error", "err", err)
return nil, err
}
return convToEventRetrieved(&eventDTO)
}
func convToEventList(eventsDTO []sqlc.ListEventsByUserIDRow) ([]model.EventListRetrieved, error) {
var events []model.EventListRetrieved
for _, evDTO := range eventsDTO {
var owner model.UserBaseRetrieved
err := json.Unmarshal(evDTO.Owner, &owner)
if err != nil {
// Unexpected
log.ErrorLog("json unmarshal error", "err", err)
return nil, err
}
ev := model.EventListRetrieved{
ID: int(evDTO.ID),
Name: evDTO.Name,
Description: evDTO.Description.String,
Owner: &owner,
CreatedAt: evDTO.CreatedAt,
}
events = append(events, ev)
}
return events, nil
}
// ListEventsByUserID implements repo.EventRepository.
func (e *eventRepository) ListEventsByUserID(
ctx context.Context,
userID int,
) ([]model.EventListRetrieved, error) {
eventsDTO, err := e.queries.ListEventsByUserID(ctx, int32(userID))
if err != nil {
log.ErrorLog("query error", "err", err)
return nil, err
}
return convToEventList(eventsDTO)
}
// UpdateInfo implements repo.EventRepository.
func (e *eventRepository) UpdateEventByID(
ctx context.Context,
event *model.EventUpdateEntity,
) error {
err := e.queries.UpdateEventByID(ctx, sqlc.UpdateEventByIDParams{
ID: int32(event.ID),
Name: event.Name,
Description: sql.NullString{String: event.Description, Valid: true},
UpdatedAt: time.Now(),
})
return err
}