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" "context"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"time"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/repo/sqlc" "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/model"
@ -51,11 +52,6 @@ func (e *eventRepository) Create(
panic("unimplemented") panic("unimplemented")
} }
// Delete implements repo.EventRepository.
func (e *eventRepository) Delete() {
panic("unimplemented")
}
// GetByID implements repo.EventRepository. // GetByID implements repo.EventRepository.
func (e *eventRepository) GetByID(ctx context.Context, eventID int) (*model.EventRetrieved, error) { func (e *eventRepository) GetByID(ctx context.Context, eventID int) (*model.EventRetrieved, error) {
queries := sqlc.New(e.db) queries := sqlc.New(e.db)
@ -66,8 +62,8 @@ func (e *eventRepository) GetByID(ctx context.Context, eventID int) (*model.Even
} }
// marshal owner and users // marshal owner and users
var owner *model.UserBaseRetrieved var owner model.UserBaseRetrieved
err = json.Unmarshal(eventDTO.Owner, owner) err = json.Unmarshal(eventDTO.Owner, &owner)
if err != nil { if err != nil {
// Unexpected // Unexpected
log.ErrorLog("json unmarshal error", "err", err) 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), DefaultCurrency: model.Currency(eventDTO.DefaultCurrency),
CreatedAt: eventDTO.CreatedAt, CreatedAt: eventDTO.CreatedAt,
UpdatedAt: eventDTO.UpdatedAt, UpdatedAt: eventDTO.UpdatedAt,
Owner: owner, Owner: &owner,
Users: users, Users: users,
} }
@ -104,16 +100,47 @@ func (e *eventRepository) GetByID(ctx context.Context, eventID int) (*model.Even
func (e *eventRepository) ListEventsByUserID( func (e *eventRepository) ListEventsByUserID(
ctx context.Context, ctx context.Context,
userID int, userID int,
) ([]model.EventBaseItemEntity, error) { ) ([]*model.EventListRetrieved, error) {
panic("unimplemented") 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. var events []*model.EventListRetrieved
func (e *eventRepository) ListExpensesByUserID() {
panic("unimplemented") 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. // UpdateInfo implements repo.EventRepository.
func (e *eventRepository) UpdateInfo() { func (e *eventRepository) UpdateEventByID(
panic("unimplemented") 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) // {{{ Entity (DB In)
type EventBaseItemEntity struct {
ID int
Name string
Description string
OwnerID int
CreatedAt time.Time
}
type EventEntity struct { type EventEntity struct {
ID int ID int
@ -84,6 +76,14 @@ type EventEntity struct {
UpdatedAt time.Time 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) // {{{ Retrieved (DB out)
@ -103,6 +103,14 @@ type EventRetrieved struct {
UpdatedAt time.Time UpdatedAt time.Time
} }
type EventListRetrieved struct {
ID int
Name string
Description string
CreatedAt time.Time
Owner *UserBaseRetrieved
}
// }}} // }}}
// {{{ DO Domain Object (Contains the domain service) // {{{ DO Domain Object (Contains the domain service)

View File

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