Skip to content

Commit

Permalink
fix bug match lover
Browse files Browse the repository at this point in the history
  • Loading branch information
le-xuan-quynh committed May 4, 2022
1 parent 419d264 commit 3909cc1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 42 deletions.
5 changes: 1 addition & 4 deletions cmd/authorization/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ const matchLoveSchema = `
userid Varchar(36) not null,
matchid Varchar(36) not null,
createdat Timestamp not null,
updatedat Timestamp not null,
Primary Key (id),
Constraint fk_user_id Foreign Key(userid) References users(id)
On Delete Cascade On Update Cascade
Primary Key (id)
)
`

Expand Down
48 changes: 18 additions & 30 deletions internal/database/postgres-repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,10 @@ func (repo *postgresRepository) InsertOrUpdateLimitData(ctx context.Context, lim
isInsert := false
if err != nil {
isInsert = true
limitData.ID = uuid.NewV4().String()
limitData.CreatedAt = time.Now()
}
limitData.ID = uuid.NewV4().String()
limitData.CreatedAt = time.Now()

limitData.UpdatedAt = time.Now()
// Insert or update
if isInsert {
Expand Down Expand Up @@ -334,9 +335,9 @@ func (repo *postgresRepository) InsertOrUpdateMatchVerifyData(ctx context.Contex
isInsert := false
if err != nil {
isInsert = true
matchData.ID = uuid.NewV4().String()
matchData.CreatedAt = time.Now()
}
matchData.ID = uuid.NewV4().String()
matchData.CreatedAt = time.Now()
matchData.UpdatedAt = time.Now()
// Insert or update
if isInsert {
Expand Down Expand Up @@ -386,36 +387,23 @@ func (repo *postgresRepository) GetMatchLoveDataByUserID(ctx context.Context, us
return matchData, err
}

// InsertOrUpdateMatchLoveData Insert or update match love data
func (repo *postgresRepository) InsertOrUpdateMatchLoveData(ctx context.Context, matchData *MatchLoveData) error {
// Check exist or not match data
_, err := repo.GetMatchLoveDataByUserID(ctx, matchData.UserID)
isInsert := false
if err != nil {
isInsert = true

}
matchData.ID = uuid.NewV4().String()
matchData.CreatedAt = time.Now()
matchData.UpdatedAt = time.Now()
// Insert or update
if isInsert {
// Insert the match data
query := "insert into matchloves(id, userid, matchid, createdat, updatedat) values($1, $2, $3, $4, $5)"
_, err := repo.db.ExecContext(ctx, query,
matchData.ID,
matchData.UserID,
matchData.MatchID,
matchData.CreatedAt,
matchData.UpdatedAt)
// InsertOrDeleteMatchLoveData Insert or delete match love data
func (repo *postgresRepository) InsertOrDeleteMatchLoveData(ctx context.Context, matchData *MatchLoveData, isDelete bool) error {
// if delete
if isDelete {
query := "delete from matchloves where userid = $1 or matchid = $1"
_, err := repo.db.ExecContext(ctx, query, matchData.UserID)
return err
} else {
// Update the match data
query := "update matchloves set matchid = $1, updatedat = $2 where userid = $3"
// insert new row
matchData.ID = uuid.NewV4().String()
matchData.CreatedAt = time.Now()
query := "insert into matchloves(id, userid, matchid, createdat) values($1, $2, $3, $4)"
_, err := repo.db.ExecContext(ctx, query,
matchData.ID,
matchData.UserID,
matchData.MatchID,
matchData.UpdatedAt,
matchData.UserID)
matchData.CreatedAt)
return err
}
}
4 changes: 2 additions & 2 deletions internal/database/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ type UserRepository interface {
DeleteMatchVerifyDataByUserID(ctx context.Context, userID string) error
// GetMatchLoveDataByUserID Get match love data
GetMatchLoveDataByUserID(ctx context.Context, userID string) (*MatchLoveData, error)
// InsertOrUpdateMatchLoveData Insert or update match love data
InsertOrUpdateMatchLoveData(ctx context.Context, matchData *MatchLoveData) error
// InsertOrDeleteMatchLoveData Insert or update match love data
InsertOrDeleteMatchLoveData(ctx context.Context, matchData *MatchLoveData, isDelete bool) error
}
1 change: 0 additions & 1 deletion internal/database/verification-data.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ type MatchLoveData struct {
UserID string `json:"userid" sql:"userid"`
MatchID string `json:"matchid" sql:"matchid"`
CreatedAt time.Time `json:"createdat" sql:"createdat"`
UpdatedAt time.Time `json:"updatedat" sql:"updatedat"`
}
2 changes: 1 addition & 1 deletion internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (e ErrorResponse) Error() string {
case UserAlreadyMatched:
return "user already matched"
case MatchCodeIsNotFound:
return "match code is not found"
return "match code is not exactly"
case UserNotMatch:
return "user not match"
default:
Expand Down
7 changes: 3 additions & 4 deletions pkg/authorization/users-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ func (s *userService) MatchLover(ctx context.Context, request *MatchLoverRequest
// Match user with new love
matchLove.UserID = user.ID
matchLove.MatchID = matchData.UserID
err = s.repo.InsertOrUpdateMatchLoveData(ctx, matchLove)
err = s.repo.InsertOrDeleteMatchLoveData(ctx, matchLove, false)
if err != nil {
s.logger.Error("Cannot insert or update match love data", "error", err)
cusErr := utils.NewErrorResponse(utils.InternalServerError)
Expand Down Expand Up @@ -1066,9 +1066,8 @@ func (s *userService) UnMatchedLover(ctx context.Context) error {
cusErr := utils.NewErrorResponse(utils.UserNotMatch)
return cusErr
}
// Delete match data or update match data with code is empty
matchLove.MatchID = ""
err = s.repo.InsertOrUpdateMatchLoveData(ctx, matchLove)
// Delete match data
err = s.repo.InsertOrDeleteMatchLoveData(ctx, matchLove, true)
if err != nil {
s.logger.Error("Cannot delete match love data", "error", err)
cusErr := utils.NewErrorResponse(utils.InternalServerError)
Expand Down

0 comments on commit 3909cc1

Please sign in to comment.