feat: implement event repo methods

This commit is contained in:
Muyao CHEN 2024-10-20 23:27:17 +02:00
parent 74ae6b7877
commit 304651e7ff
3 changed files with 63 additions and 31 deletions

View File

@ -26,6 +26,7 @@ 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"
@ -51,11 +52,6 @@ func (e *eventRepository) Create(
panic("unimplemented")
}
// Delete implements repo.EventRepository.
func (e *eventRepository) Delete() {
panic("unimplemented")
}
// GetByID implements repo.EventRepository.
func (e *eventRepository) GetByID(ctx context.Context, eventID int) (*model.EventRetrieved, error) {
queries := sqlc.New(e.db)
@ -66,8 +62,8 @@ func (e *eventRepository) GetByID(ctx context.Context, eventID int) (*model.Even
}
// marshal owner and users
var owner *model.UserBaseRetrieved
err = json.Unmarshal(eventDTO.Owner, owner)
var owner model.UserBaseRetrieved
err = json.Unmarshal(eventDTO.Owner, &owner)
if err != nil {
// Unexpected
log.ErrorLog("json unmarshal error", "err", err)
@ -93,7 +89,7 @@ func (e *eventRepository) GetByID(ctx context.Context, eventID int) (*model.Even
DefaultCurrency: model.Currency(eventDTO.DefaultCurrency),
CreatedAt: eventDTO.CreatedAt,
UpdatedAt: eventDTO.UpdatedAt,
Owner: owner,
Owner: &owner,
Users: users,
}
@ -104,16 +100,47 @@ func (e *eventRepository) GetByID(ctx context.Context, eventID int) (*model.Even
func (e *eventRepository) ListEventsByUserID(
ctx context.Context,
userID int,
) ([]model.EventBaseItemEntity, error) {
panic("unimplemented")
}
) ([]*model.EventListRetrieved, error) {
queries := sqlc.New(e.db)
eventsDTO, err := queries.ListEventsByUserID(ctx, int32(userID))
if err != nil {
log.ErrorLog("query error", "err", err)
return nil, err
}
// ListExpensesByUserID implements repo.EventRepository.
func (e *eventRepository) ListExpensesByUserID() {
panic("unimplemented")
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,
}
events = append(events, ev)
}
return events, nil
}
// UpdateInfo implements repo.EventRepository.
func (e *eventRepository) UpdateInfo() {
panic("unimplemented")
func (e *eventRepository) UpdateEventByID(
ctx context.Context,
event *model.EventUpdateEntity,
) error {
queries := sqlc.New(e.db)
err := 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
}

View File

@ -63,14 +63,6 @@ type EventInfoResponse struct {
// }}}
// {{{ Entity (DB In)
type EventBaseItemEntity struct {
ID int
Name string
Description string
OwnerID int
CreatedAt time.Time
}
type EventEntity struct {
ID int
@ -84,6 +76,14 @@ type EventEntity struct {
UpdatedAt time.Time
}
type EventUpdateEntity struct {
ID int
Name string
Description string
CreatedAt time.Time
// TODO: maybe I can change owner too
}
// }}}
// {{{ Retrieved (DB out)
@ -103,6 +103,14 @@ type EventRetrieved struct {
UpdatedAt time.Time
}
type EventListRetrieved struct {
ID int
Name string
Description string
CreatedAt time.Time
Owner *UserBaseRetrieved
}
// }}}
// {{{ DO Domain Object (Contains the domain service)

View File

@ -31,17 +31,13 @@ import (
type EventRepository interface {
Create(ctx context.Context, evEntity *model.EventEntity) (*model.EventEntity, error)
// UpdateInfo updates the event related information (name, descriptions)
UpdateInfo()
Delete() // XXX: Pay attention to the foreign key relationships
// UpdateEventByID updates the event related information (name, descriptions)
UpdateEventByID(ctx context.Context, event *model.EventUpdateEntity) error
GetByID(ctx context.Context, eventID int) (*model.EventRetrieved, error)
ListExpensesByUserID()
// related to events of a user
ListEventsByUserID(ctx context.Context, userID int) ([]model.EventBaseItemEntity, error)
ListEventsByUserID(ctx context.Context, userID int) ([]*model.EventListRetrieved, error)
}
type ExpenseRepository interface {
@ -49,6 +45,7 @@ type ExpenseRepository interface {
Update()
Delete() // Delete also the related transactions
ListExpensesByUserID()
GetByID()
}