99 "strconv"
1010 "time"
1111
12+ "github.com/hammer-code/lms-be/constants"
1213 "github.com/hammer-code/lms-be/domain"
1314 contextkey "github.com/hammer-code/lms-be/pkg/context_key"
1415 "github.com/hammer-code/lms-be/pkg/email"
@@ -19,6 +20,20 @@ import (
1920)
2021
2122func (uc usecase ) CreateRegistrationEvent (ctx context.Context , payload domain.RegisterEventPayload ) (domain.RegisterEventResponse , error ) {
23+ // get user data from context
24+ userData := ctx .Value (contextkey .UserKey ).(domain.User )
25+
26+ registrations , err := uc .repository .GetRegistrationEventUserByStatus (ctx , payload .EventID , uint (userData .ID ), []string {constants .PENDING , constants .SUCCESS })
27+ if err != nil {
28+ err = utils .NewInternalServerError (ctx , err )
29+ return domain.RegisterEventResponse {}, err
30+ }
31+
32+ if len (registrations ) != 0 {
33+ err = utils .NewBadRequestError (ctx , "you have already registered" , errors .New ("you have already registered" ))
34+ return domain.RegisterEventResponse {}, err
35+ }
36+
2237 event , err := uc .repository .GetEvent (ctx , payload .EventID )
2338 if err != nil {
2439 err = utils .NewInternalServerError (ctx , err )
@@ -59,9 +74,6 @@ func (uc usecase) CreateRegistrationEvent(ctx context.Context, payload domain.Re
5974 }
6075 }
6176
62- // get user data from context
63- userData := ctx .Value (contextkey .UserKey ).(domain.User )
64-
6577 // generate order number
6678 // format: TXE-<event_id>-<year><month><day><hash
6779 hash := hash .GenerateHash (time .Now ().Format ("2006-01-02 15:04:05" ))
@@ -124,9 +136,9 @@ func (uc usecase) CreateRegistrationEvent(ctx context.Context, payload domain.Re
124136 }
125137
126138 // is free event or not
127- status := " SUCCESS"
139+ status := constants . SUCCESS
128140 if event .Price != 0.0 {
129- status = " PENDING"
141+ status = constants . PENDING
130142 emailPayload .SendEmail (ctx )
131143 } else {
132144 logrus .Info ("free event, send email registration success" )
0 commit comments