Compare commits

..

No commits in common. "0dbf5c623402e4386db9c950805edd2fdc9ee225" and "dde4eb337cbeec08ac489d40ce521f04a532dd26" have entirely different histories.

19 changed files with 96 additions and 205 deletions

View File

@ -422,7 +422,7 @@ The following basic use cases are to be implemented at the first time.
- [X] A user signs up
- [X] A user logs in
- [X] A user lists their events (pagination)
- [X] A user sees the detail of an event (description, members, amount)
- [] A user sees the detail of an event (description, members, amount)
- [] A user sees the expenses of an event (total amount, personal expenses, pagination)
- [] A user sees the detail of an expense: (time, amount, payers, recipients)
- [] A user adds an expense
@ -431,18 +431,13 @@ The following basic use cases are to be implemented at the first time.
- [] A user restore a deleted expense
- [] A user can pay the debt to other members
- [X] A user creates an event (and participate to it)
- [X] A user updates the event info
- [X] A user invites another user by sending a mail with a token.
- [X] A user joins an event by accepting an invitation
- [] ~A user quits an event (they cannot actually, but we can make as if they quitted)~
**No we can't quit!**
- [] A user updates the event info
- [] A user invites another user by sending a mail with a token.
- [] A user joins an event by accepting an invitation
- [] A user quits an event (they cannot actually, but we can make as if they quitted)
- [] A user cannot see other user's information
- [] A user cannot see the events that they didn't participated in.
For the second stage:
- [] A user can archive an event
With those functionalities, there will be an usable product. And then we can
work on other aspects. For example:

View File

@ -64,7 +64,7 @@ type createParams struct {
// Since we use JWT method, this token is not stored anywhere. Thus it
// stops at the controller level.
func (sc *SessionController) Create(ctx *gin.Context) {
var user model.UserExistRequest
var user model.UserExistDTO
if err := ctx.Bind(&user); err != nil {
log.ErrorLog("param error", "err", err)

View File

@ -37,12 +37,12 @@ func NewtestUserUsecase() usecase.User {
func (*testUserUsecase) Create(
ctx context.Context,
u *model.UserCreateRequest,
) (*model.UserInfoResponse, error) {
u *model.UserCreateDTO,
) (*model.UserInfoVO, error) {
return nil, nil
}
func (*testUserUsecase) Exist(ctx context.Context, u *model.UserExistRequest) error {
func (*testUserUsecase) Exist(ctx context.Context, u *model.UserExistDTO) error {
switch u.Email {
case "a@b.c":
if u.Password == "strong password" {
@ -57,10 +57,10 @@ func (*testUserUsecase) Exist(ctx context.Context, u *model.UserExistRequest) er
return nil
}
func (*testUserUsecase) GetUserBaseResponseByID(
func (*testUserUsecase) GetUserBaseVOByID(
ctx context.Context,
userID int,
) (*model.UserBaseResponse, error) {
) (*model.UserBaseVO, error) {
// TODO:
return nil, nil
}

View File

@ -57,14 +57,14 @@ func NewUserController(us usecase.User) User {
}
func (uc *UserController) Create(ctx core.Context) {
var userRequest model.UserCreateRequest
var userDTO model.UserCreateDTO
if err := ctx.Bind(&userRequest); err != nil {
if err := ctx.Bind(&userDTO); err != nil {
core.WriteResponse(ctx, UserParamsErr, nil)
return
}
_, err := uc.userUsecase.Create(ctx, &userRequest)
_, err := uc.userUsecase.Create(ctx, &userDTO)
if err != nil {
core.WriteResponse(ctx, err, nil)
return

View File

@ -56,9 +56,3 @@ GROUP BY
e.total_amount, e.default_currency,
o.id, o.first_name, o.last_name;
-- name: UpdateEventByID :exec
UPDATE "event"
SET name = $2, description = $3, updated_at = $4
WHERE id = $1;

View File

@ -172,26 +172,3 @@ func (q *Queries) ListEventsByUserID(ctx context.Context, userID int32) ([]ListE
}
return items, nil
}
const updateEventByID = `-- name: UpdateEventByID :exec
UPDATE "event"
SET name = $2, description = $3, updated_at = $4
WHERE id = $1
`
type UpdateEventByIDParams struct {
ID int32
Name string
Description sql.NullString
UpdatedAt time.Time
}
func (q *Queries) UpdateEventByID(ctx context.Context, arg UpdateEventByIDParams) error {
_, err := q.db.ExecContext(ctx, updateEventByID,
arg.ID,
arg.Name,
arg.Description,
arg.UpdatedAt,
)
return err
}

View File

@ -5,7 +5,6 @@
package sqlc
import (
"database/sql"
"time"
)
@ -16,26 +15,6 @@ type Admin struct {
AccessLevel int32
}
type Event struct {
ID int32
Name string
Description sql.NullString
DefaultCurrency string
OwnerID int32
CreatedAt time.Time
UpdatedAt time.Time
TotalAmount sql.NullInt32
}
type Participation struct {
ID int32
UserID int32
EventID int32
InvitedByUserID sql.NullInt32
CreatedAt time.Time
UpdatedAt time.Time
}
type User struct {
ID int32
Email string

View File

@ -50,8 +50,8 @@ func NewUserRepository(db *sql.DB) repo.UserRepository {
func (ur *userRepository) Create(
ctx context.Context,
transaction interface{},
u *model.UserEntity ,
) (*model.UserEntity , error) {
u *model.UserPO,
) (*model.UserPO, error) {
timeoutCtx, cancel := context.WithTimeout(ctx, insertTimeout)
defer cancel()
@ -76,7 +76,7 @@ func (ur *userRepository) Create(
return nil, err
}
return &model.UserEntity {
return &model.UserPO{
ID: int(userDB.ID),
Email: userDB.Email,
FirstName: userDB.FirstName,
@ -88,7 +88,7 @@ func (ur *userRepository) Create(
}
// GetByEmail if not found, return nil for user but not error.
func (ur *userRepository) GetByEmail(ctx context.Context, email string) (*model.UserEntity , error) {
func (ur *userRepository) GetByEmail(ctx context.Context, email string) (*model.UserPO, error) {
queries := sqlc.New(ur.db)
userDB, err := queries.GetUserByEmail(ctx, email)
if errors.Is(err, pgx.ErrNoRows) {
@ -98,7 +98,7 @@ func (ur *userRepository) GetByEmail(ctx context.Context, email string) (*model.
return nil, err
}
return &model.UserEntity {
return &model.UserPO{
ID: int(userDB.ID),
Email: userDB.Email,
FirstName: userDB.FirstName,
@ -109,7 +109,7 @@ func (ur *userRepository) GetByEmail(ctx context.Context, email string) (*model.
}, nil
}
func (ur *userRepository) GetByID(ctx context.Context, id int) (*model.UserEntity , error) {
func (ur *userRepository) GetByID(ctx context.Context, id int) (*model.UserPO, error) {
queries := sqlc.New(ur.db)
userDB, err := queries.GetUserByID(ctx, int32(id))
if errors.Is(err, pgx.ErrNoRows) {
@ -119,7 +119,7 @@ func (ur *userRepository) GetByID(ctx context.Context, id int) (*model.UserEntit
return nil, err
}
return &model.UserEntity {
return &model.UserPO{
ID: int(userDB.ID),
Email: userDB.Email,
FirstName: userDB.FirstName,

View File

@ -24,9 +24,9 @@ package model
import "time"
// {{{ Request Object (from controller to service)
// {{{ DTO Data Transfer Object (from controller to service)
type EventCreateRequest struct {
type EventCreateDTO struct {
Name string `json:"name" binding:"requiered"`
Description string `json:"description"`
OwnerID int `json:"owner_id" binding:"requiered,number"`
@ -34,17 +34,9 @@ type EventCreateRequest struct {
}
// }}}
// {{{ Response View Object (from service to controller)
// {{{ VO View Object (from service to controller)
type EventBaseItemResponse struct {
ID int
Name string
Description string
Owner *UserBaseResponse
CreatedAt time.Time
}
type EventInfoResponse struct {
type EventInfoVO struct {
ID int
Name string
@ -52,26 +44,18 @@ type EventInfoResponse struct {
TotalAmount Money
Owner *UserBaseResponse
Owner *UserBaseVO
CreatedAt time.Time
UpdatedAt time.Time
Users []*UserBaseResponse
Users []*UserBaseVO
}
// }}}
// {{{ Entity Persistant Object (Between the service and the repository)
// {{{ PO Persistant Object (Between the service and the repository)
type EventBaseItemEntity struct {
ID int
Name string
Description string
OwnerID int
CreatedAt time.Time
}
type EventEntity struct {
type EventPO struct {
ID int
Name string

View File

@ -24,7 +24,7 @@ package model
import "time"
type ExpenseRequest struct {
type ExpenseDTO struct {
Amount Money `json:"money" binding:"required,number"`
PayerIDs []int `json:"payer_ids" binding:"required"`
RecipientIDs []int `json:"recipient_ids" binding:"required"`
@ -32,7 +32,7 @@ type ExpenseRequest struct {
Detail ExpenseDetail `json:"detail"`
}
type ExpenseEntity struct {
type ExpensePO struct {
ID int
Amount int

View File

@ -24,7 +24,7 @@ package model
import "time"
type ParticipationEntity Participation
type ParticipationPO Participation
// Participation is the association between Users and Events
type Participation struct {

View File

@ -24,7 +24,7 @@ package model
import "time"
type TransactionEntity Transaction
type TransactionPO Transaction
// Transaction is the association between Expenses and Users
type Transaction struct {

View File

@ -24,31 +24,31 @@ package model
import "time"
// {{{ Request (from controller to service)
// {{{ DTO Data Transfer Object (from controller to service)
type UserCreateRequest struct {
type UserCreateDTO struct {
Email string `json:"email" binding:"required,email"`
FirstName string `json:"first_name" binding:"required"`
LastName string `json:"last_name" binding:"required"`
Password string `json:"password" binding:"required"`
}
type UserExistRequest struct {
type UserExistDTO struct {
Email string `json:"email" binding:"required,email"`
Password string `json:"password" binding:"required"`
}
// }}}
// {{{ Response View Object (from service to controller)
// {{{ VO View Object (from service to controller)
type UserBaseResponse struct {
type UserBaseVO struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
type UserInfoResponse struct {
// UserBaseResponse
type UserInfoVO struct {
// UserBaseVO
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
@ -59,9 +59,9 @@ type UserInfoResponse struct {
}
// }}}
// {{{ Entity Persistant Object (Between the service and the repository)
// {{{ PO Persistant Object (Between the service and the repository)
type UserEntity struct {
type UserPO struct {
ID int
Email string

View File

@ -29,19 +29,19 @@ import (
)
type EventRepository interface {
Create(ctx context.Context, evEntity *model.EventEntity) (*model.EventEntity, error)
Create(ctx context.Context, evPO *model.EventPO) (*model.EventPO, error)
// UpdateInfo updates the event related information (name, descriptions)
UpdateInfo()
Delete() // XXX: Pay attention to the foreign key relationships
GetByID(ctx context.Context, eventID int) (*model.EventEntity, error)
GetByID()
ListExpensesByUserID()
// related to events of a user
ListEventsByUserID(ctx context.Context, userID int) ([]model.EventBaseItemEntity, error)
ListEventsByUserID()
}
type ExpenseRepository interface {
@ -55,7 +55,6 @@ type ExpenseRepository interface {
type ParticipationRepository interface {
Create()
Delete()
CheckParticipation(ctx context.Context, userID, eventID int) error
}
type TransactionRepository interface {

View File

@ -29,7 +29,7 @@ import (
)
type UserRepository interface {
Create(ctx context.Context, transaction interface{}, u *model.UserEntity ) (*model.UserEntity , error)
GetByEmail(ctx context.Context, email string) (*model.UserEntity , error)
GetByID(ctx context.Context, id int) (*model.UserEntity , error)
Create(ctx context.Context, transaction interface{}, u *model.UserPO) (*model.UserPO, error)
GetByEmail(ctx context.Context, email string) (*model.UserPO, error)
GetByID(ctx context.Context, id int) (*model.UserPO, error)
}

View File

@ -23,11 +23,8 @@
package usecase
import (
"net/http"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/usecase/repo"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
"golang.org/x/net/context"
)
@ -42,12 +39,6 @@ type eventUsecase struct {
dbRepo repo.DBRepository
}
var ErrNoParticipation = &errno.Errno{
HTTP: http.StatusUnauthorized,
Code: errno.ErrorCode(errno.AuthFailureCode, "NoParticipation"),
Message: "user doesn't have access to this event",
}
// For the controller
type Event interface{}
@ -55,8 +46,8 @@ func NewEventUsecase(
uuc User,
ev repo.EventRepository,
ex repo.ExpenseRepository,
pa repo.ParticipationRepository, // XXX: Might be handled in event
tr repo.TransactionRepository, // XXX: Might be handled in event
pa repo.ParticipationRepository,
tr repo.TransactionRepository,
db repo.DBRepository,
) Event {
return &eventUsecase{uuc, ev, ex, pa, tr, db}
@ -64,22 +55,22 @@ func NewEventUsecase(
func (evuc *eventUsecase) CreateEvent(
ctx context.Context,
evRequest *model.EventCreateRequest,
) (*model.EventInfoResponse, error) {
// transfer evRequest to PO
evDTO *model.EventCreateDTO,
) (*model.EventInfoVO, error) {
// transfer evDTO to PO
evEntity := &model.EventEntity{
Name: evRequest.Name,
Description: evRequest.Description,
OwnerID: evRequest.OwnerID,
evPO := &model.EventPO{
Name: evDTO.Name,
Description: evDTO.Description,
OwnerID: evDTO.OwnerID,
TotalAmount: 0,
DefaultCurrency: string(evRequest.DefaultCurrency),
DefaultCurrency: string(evDTO.DefaultCurrency),
}
data, err := evuc.dbRepo.Transaction(
ctx,
func(txCtx context.Context, tx interface{}) (interface{}, error) {
created, err := evuc.eventRepo.Create(ctx, evEntity)
created, err := evuc.eventRepo.Create(ctx, evPO)
if err != nil {
return nil, err
}
@ -93,12 +84,12 @@ func (evuc *eventUsecase) CreateEvent(
created.OwnerID,
)
ownerResponse, err := evuc.userUC.GetUserBaseResponseByID(ctx, created.OwnerID)
ownerVO, err := evuc.userUC.GetUserBaseVOByID(ctx, created.OwnerID)
if err != nil {
return nil, err
}
evResponse := &model.EventInfoResponse{
evVO := &model.EventInfoVO{
ID: created.ID,
Name: created.Name,
Description: created.Description,
@ -106,40 +97,16 @@ func (evuc *eventUsecase) CreateEvent(
created.TotalAmount,
model.Currency(created.DefaultCurrency),
),
Owner: ownerResponse,
Owner: ownerVO,
CreatedAt: created.CreatedAt,
}
return evResponse, err
return evVO, err
})
if err != nil {
return nil, err
}
res := data.(*model.EventInfoResponse)
res := data.(*model.EventInfoVO)
return res, err
}
func (evuc *eventUsecase) ListEvents(
ctx context.Context,
userID int,
) ([]model.EventBaseItemResponse, error) {
return nil, nil
}
// GetEventDetail
func (evuc *eventUsecase) GetEventDetail(
ctx context.Context,
userID, eventID int,
) (*model.EventInfoResponse, error) {
// Check if the user has the right to get this event
err := evuc.participationRepo.CheckParticipation(ctx, userID, eventID)
if err != nil {
return nil, ErrNoParticipation
}
// Get the eventDetail
// TODO: This can also be put into the cache
return nil, nil
}

View File

@ -37,8 +37,8 @@ type TestUserRepository struct{}
func (tur *TestUserRepository) Create(
ctx context.Context,
transaction interface{},
u *model.UserEntity,
) (*model.UserEntity, error) {
u *model.UserPO,
) (*model.UserPO, error) {
user := *u
user.ID = 123
@ -53,11 +53,11 @@ func (tur *TestUserRepository) Create(
func (tur *TestUserRepository) GetByEmail(
ctx context.Context,
email string,
) (*model.UserEntity, error) {
) (*model.UserPO, error) {
hashedPwd, _ := bcrypt.GenerateFromPassword([]byte("strongHashed"), 12)
switch email {
case "a@b.c":
return &model.UserEntity{
return &model.UserPO{
ID: 123,
Email: "a@b.c",
Password: string(hashedPwd),
@ -71,11 +71,11 @@ func (tur *TestUserRepository) GetByEmail(
return nil, UserTestDummyErr
}
func (tur *TestUserRepository) GetByID(ctx context.Context, id int) (*model.UserEntity, error) {
func (tur *TestUserRepository) GetByID(ctx context.Context, id int) (*model.UserPO, error) {
hashedPwd, _ := bcrypt.GenerateFromPassword([]byte("strongHashed"), 12)
switch id {
case 123:
return &model.UserEntity{
return &model.UserPO{
ID: 123,
Email: "a@b.c",
Password: string(hashedPwd),

View File

@ -60,9 +60,9 @@ type userUsecase struct {
}
type User interface {
Create(ctx context.Context, u *model.UserCreateRequest) (*model.UserInfoResponse, error)
Exist(ctx context.Context, u *model.UserExistRequest) error
GetUserBaseResponseByID(ctx context.Context, userID int) (*model.UserBaseResponse, error)
Create(ctx context.Context, u *model.UserCreateDTO) (*model.UserInfoVO, error)
Exist(ctx context.Context, u *model.UserExistDTO) error
GetUserBaseVOByID(ctx context.Context, userID int) (*model.UserBaseVO, error)
}
func NewUserUsecase(r repo.UserRepository, d repo.DBRepository) User {
@ -74,8 +74,8 @@ func NewUserUsecase(r repo.UserRepository, d repo.DBRepository) User {
func (uuc *userUsecase) Create(
ctx context.Context,
u *model.UserCreateRequest,
) (*model.UserInfoResponse, error) {
u *model.UserCreateDTO,
) (*model.UserInfoVO, error) {
// Hash the password
encrypted, err := bcrypt.GenerateFromPassword([]byte(u.Password), 12)
if err != nil {
@ -86,7 +86,7 @@ func (uuc *userUsecase) Create(
data, err := uuc.dbRepo.Transaction(
ctx,
func(txCtx context.Context, tx interface{}) (interface{}, error) {
created, err := uuc.userRepo.Create(txCtx, tx, &model.UserEntity{
created, err := uuc.userRepo.Create(txCtx, tx, &model.UserPO{
Email: u.Email,
Password: u.Password,
FirstName: u.FirstName,
@ -117,21 +117,21 @@ func (uuc *userUsecase) Create(
return nil, err
}
userEntity := data.(*model.UserEntity)
userPO := data.(*model.UserPO)
user := &model.UserInfoResponse{
ID: userEntity.ID,
Email: userEntity.Email,
FirstName: userEntity.FirstName,
LastName: userEntity.LastName,
CreatedAt: userEntity.CreatedAt,
UpdatedAt: userEntity.UpdatedAt,
user := &model.UserInfoVO{
ID: userPO.ID,
Email: userPO.Email,
FirstName: userPO.FirstName,
LastName: userPO.LastName,
CreatedAt: userPO.CreatedAt,
UpdatedAt: userPO.UpdatedAt,
}
return user, nil
}
func (uuc *userUsecase) Exist(ctx context.Context, u *model.UserExistRequest) error {
func (uuc *userUsecase) Exist(ctx context.Context, u *model.UserExistDTO) error {
got, err := uuc.userRepo.GetByEmail(ctx, u.Email)
// Any query error?
if err != nil {
@ -152,19 +152,15 @@ func (uuc *userUsecase) Exist(ctx context.Context, u *model.UserExistRequest) er
return nil
}
func (uuc *userUsecase) GetUserBaseResponseByID(
func (uuc *userUsecase) GetUserBaseVOByID(
ctx context.Context,
userID int,
) (*model.UserBaseResponse, error) {
// TODO: should try first to get from the cache
// If not exists, get from the DB. And then put back
// into the cache with a timeout.
// Refresh the cache when the user data is updated (for now it cannot be updated)
) (*model.UserBaseVO, error) {
got, err := uuc.userRepo.GetByID(ctx, userID)
if err != nil {
return nil, err
}
userBaseVo := &model.UserBaseResponse{
userBaseVo := &model.UserBaseVO{
ID: got.ID,
FirstName: got.FirstName,
LastName: got.LastName,

View File

@ -35,13 +35,13 @@ func TestCreateUser(t *testing.T) {
t.Run("normal create", func(t *testing.T) {
ctx := context.Background()
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
input := &model.UserCreateRequest{
input := &model.UserCreateDTO{
Email: "a@b.c",
FirstName: "James",
LastName: "Bond",
Password: "verystrong",
}
want := &model.UserInfoResponse{
want := &model.UserInfoVO{
ID: 123,
}
@ -53,7 +53,7 @@ func TestCreateUser(t *testing.T) {
t.Run("duplicate create", func(t *testing.T) {
ctx := context.Background()
userUsecase := NewUserUsecase(&repomock.TestUserRepository{}, &repomock.TestDBRepository{})
input := &model.UserCreateRequest{
input := &model.UserCreateDTO{
Email: "duplicate@error.com",
FirstName: "James",
LastName: "Bond",
@ -68,22 +68,22 @@ func TestCreateUser(t *testing.T) {
func TestUserExist(t *testing.T) {
testCases := []struct {
Name string
User *model.UserExistRequest
User *model.UserExistDTO
ExpErr error
}{
{"user exists", &model.UserExistRequest{
{"user exists", &model.UserExistDTO{
Email: "a@b.c",
Password: "strongHashed",
}, nil},
{"query error", &model.UserExistRequest{
{"query error", &model.UserExistDTO{
Email: "query@error.com",
Password: "strongHashed",
}, repomock.UserTestDummyErr},
{"user doesn not exist", &model.UserExistRequest{
{"user doesn not exist", &model.UserExistDTO{
Email: "inexist@error.com",
Password: "strongHashed",
}, UserNotExist},
{"wrong password", &model.UserExistRequest{
{"wrong password", &model.UserExistDTO{
Email: "a@b.c",
Password: "wrongHashed",
}, UserWrongPassword},