Skip to content

Commit

Permalink
activity - refactor create activity & add request model
Browse files Browse the repository at this point in the history
  • Loading branch information
omerbeden committed Mar 29, 2024
1 parent c3ab674 commit 42186c7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
54 changes: 39 additions & 15 deletions backend/tatooine/cmd/api/handlers/activity_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ func getRequestId(c *fiber.Ctx) (string, error) {

func CreateActivity(service entrypoints.ActivityService) fiber.Handler {
return func(c *fiber.Ctx) error {
requestid, err := getRequestId(c)
if err != nil {
return err
}

ctx, cancel := context.WithTimeout(c.Context(), time.Second*5)
defer cancel()

logger := pkg.Logger()
newLogger := logger.With(zap.String("requestid", requestid))
ctx = context.WithValue(ctx, pkg.LoggerKey, newLogger)

var requestBody model.Activity
err := c.BodyParser(&requestBody)
var requestBody presenter.CreateActivityRequest
err = c.BodyParser(&requestBody)
if err != nil {
logger.Error(presenter.BODY_PARSER_ERR)
newLogger.Error(err)
return c.Status(fiber.StatusBadRequest).JSON(presenter.BaseResponse{
APIVersion: presenter.APIVersion,
Data: nil,
Error: presenter.BODY_PARSER_ERR,
})
}

ctx, cancel := context.WithTimeout(c.Context(), time.Second*5)
defer cancel()

requestid, err := getRequestId(c)
if err != nil {
return err
}

newLogger := logger.With(zap.String("requestid", requestid))
ctx = context.WithValue(ctx, pkg.LoggerKey, newLogger)

res, err := service.CreateActivity(ctx, requestBody)
activity := toActivity(requestBody)
res, err := service.CreateActivity(ctx, activity)
if err != nil {
logger.Error(err)

Expand All @@ -71,6 +71,30 @@ func CreateActivity(service entrypoints.ActivityService) fiber.Handler {
})
}
}
func toActivity(request presenter.CreateActivityRequest) model.Activity {
return model.Activity{
Title: request.Title,
Category: request.Category,
CreatedBy: model.User{
ID: request.CreatedById,
ExternalId: request.CreatedByExternalId,
},
Location: model.Location{
City: request.Location.Location.City,
District: request.Location.Location.District,
Description: request.Location.Description,
Latitude: request.Location.Latitude,
Longitude: request.Location.Longitude,
},
StartAt: time.Time{},
EndAt: time.Time{},
Content: request.Content,
Rules: request.Rules,
Flow: request.Flow,
Quota: request.Quota,
GenderComposition: model.GenderComposition(request.GenderComposition),
}
}

func AddParticipant(service entrypoints.ActivityService) fiber.Handler {
return func(c *fiber.Ctx) error {
Expand Down
26 changes: 26 additions & 0 deletions backend/tatooine/cmd/api/presenter/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,29 @@ type BaseResponse struct {
Data any `json:"data"`
Error string `json:"error"`
}

type CreateActivityRequest struct {
Title string `json:"title"`
Category string `json:"category"`
CreatedById int64 `json:"createdById"`
CreatedByExternalId string `json:"createdByExternalId"`
Location LocationDetail `json:"location"`
StartAt string `json:"startAt"`
EndAt string `json:"endAt"`
Content string `json:"content"`
Rules []string `json:"rules"`
Flow []string `json:"flow"`
Quota int `json:"quota"`
GenderComposition string `json:"genderComposition"`
}

type Location struct {
City string `json:"city"`
District string `json:"district"`
}
type LocationDetail struct {
Location Location `json:"location"`
Description string `json:"description"`
Latitude float32 `json:"latitude"`
Longitude float32 `json:"longitude"`
}
7 changes: 4 additions & 3 deletions backend/tatooine/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/omerbeden/event-mate/backend/tatooine

go 1.19

require github.com/go-redis/redis/v8 v8.11.5
require (
github.com/go-redis/redis/v8 v8.11.5
go.uber.org/zap v1.27.0
)

require (
cloud.google.com/go v0.110.0 // indirect
Expand All @@ -26,7 +29,6 @@ require (
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
Expand All @@ -39,7 +41,6 @@ require (
github.com/yuin/gopher-lua v1.1.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
Expand Down
7 changes: 2 additions & 5 deletions backend/tatooine/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XP
github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -109,10 +108,7 @@ github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFr
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down Expand Up @@ -154,6 +150,7 @@ github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
Expand Down
1 change: 0 additions & 1 deletion backend/tatooine/modules/activity/app/domain/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ type User struct {
ProfileImageUrl string `json:"profileImageUrl"`
ProfilePoint float64 `json:"points"`
ExternalId string `json:"externalId,omitempty"`
About string `json:"about,omitempty"`
}

0 comments on commit 42186c7

Please sign in to comment.