Skip to content

Commit

Permalink
fix: new user model (#27)
Browse files Browse the repository at this point in the history
* fix: new model

* add new source to event registration

* fix: lint
  • Loading branch information
ImSoZRious authored Jan 8, 2024
1 parent d8de910 commit 7d4c145
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 24 deletions.
49 changes: 47 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Code generated by swaggo/swag. DO NOT EDIT.

// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs

import "github.com/swaggo/swag"
Expand Down Expand Up @@ -334,6 +333,15 @@ const docTemplate = `{
"name": "scheduleId",
"in": "path",
"required": true
},
{
"description": "Event register body",
"name": "registerEventDto",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.EventRegistrationDTO"
}
}
],
"responses": {}
Expand Down Expand Up @@ -476,6 +484,27 @@ const docTemplate = `{
}
}
},
"dto.EventRegistrationDTO": {
"type": "object",
"properties": {
"news_sources": {
"type": "array",
"items": {
"type": "string"
},
"example": [
"facebook",
"instagram",
"faculty",
"chula-student",
"friend",
"parent",
"school",
"other"
]
}
}
},
"dto.Faculty": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -698,6 +727,10 @@ const docTemplate = `{
"dto.RegisterRequestDTO": {
"type": "object",
"properties": {
"allergies": {
"type": "string",
"example": "Dog"
},
"birth_date": {
"type": "string",
"example": "1990-01-01"
Expand Down Expand Up @@ -732,6 +765,10 @@ const docTemplate = `{
"type": "string",
"example": "Doe"
},
"medical_condition": {
"type": "string",
"example": "Dog"
},
"news_sources": {
"type": "array",
"items": {
Expand Down Expand Up @@ -813,6 +850,10 @@ const docTemplate = `{
"dto.User": {
"type": "object",
"properties": {
"allergies": {
"type": "string",
"example": "Dog"
},
"birth_date": {
"type": "string",
"example": "1990-01-01"
Expand Down Expand Up @@ -846,6 +887,10 @@ const docTemplate = `{
"type": "string",
"example": "Doe"
},
"medical_condition": {
"type": "string",
"example": "Dog"
},
"news_sources": {
"type": "array",
"items": {
Expand Down
46 changes: 46 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,15 @@
"name": "scheduleId",
"in": "path",
"required": true
},
{
"description": "Event register body",
"name": "registerEventDto",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/dto.EventRegistrationDTO"
}
}
],
"responses": {}
Expand Down Expand Up @@ -471,6 +480,27 @@
}
}
},
"dto.EventRegistrationDTO": {
"type": "object",
"properties": {
"news_sources": {
"type": "array",
"items": {
"type": "string"
},
"example": [
"facebook",
"instagram",
"faculty",
"chula-student",
"friend",
"parent",
"school",
"other"
]
}
}
},
"dto.Faculty": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -693,6 +723,10 @@
"dto.RegisterRequestDTO": {
"type": "object",
"properties": {
"allergies": {
"type": "string",
"example": "Dog"
},
"birth_date": {
"type": "string",
"example": "1990-01-01"
Expand Down Expand Up @@ -727,6 +761,10 @@
"type": "string",
"example": "Doe"
},
"medical_condition": {
"type": "string",
"example": "Dog"
},
"news_sources": {
"type": "array",
"items": {
Expand Down Expand Up @@ -808,6 +846,10 @@
"dto.User": {
"type": "object",
"properties": {
"allergies": {
"type": "string",
"example": "Dog"
},
"birth_date": {
"type": "string",
"example": "1990-01-01"
Expand Down Expand Up @@ -841,6 +883,10 @@
"type": "string",
"example": "Doe"
},
"medical_condition": {
"type": "string",
"example": "Dog"
},
"news_sources": {
"type": "array",
"items": {
Expand Down
34 changes: 34 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ definitions:
example: invalid-event-id
type: string
type: object
dto.EventRegistrationDTO:
properties:
news_sources:
example:
- facebook
- instagram
- faculty
- chula-student
- friend
- parent
- school
- other
items:
type: string
type: array
type: object
dto.Faculty:
properties:
code:
Expand Down Expand Up @@ -241,6 +257,9 @@ definitions:
type: object
dto.RegisterRequestDTO:
properties:
allergies:
example: Dog
type: string
birth_date:
example: "1990-01-01"
type: string
Expand All @@ -266,6 +285,9 @@ definitions:
last_name:
example: Doe
type: string
medical_condition:
example: Dog
type: string
news_sources:
example:
- facebook
Expand Down Expand Up @@ -324,6 +346,9 @@ definitions:
type: object
dto.User:
properties:
allergies:
example: Dog
type: string
birth_date:
example: "1990-01-01"
type: string
Expand All @@ -348,6 +373,9 @@ definitions:
last_name:
example: Doe
type: string
medical_condition:
example: Dog
type: string
news_sources:
example:
- facebook
Expand Down Expand Up @@ -582,6 +610,12 @@ paths:
name: scheduleId
required: true
type: integer
- description: Event register body
in: body
name: registerEventDto
required: true
schema:
$ref: '#/definitions/dto.EventRegistrationDTO'
produces:
- application/json
responses: {}
Expand Down
4 changes: 4 additions & 0 deletions internal/auth/auth.util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func ConvertRegisterRequestDTOToUser(dto *dto.RegisterRequestDTO, email string)
user.Province = dto.Province
user.Country = dto.Country
user.EducationalLevel = dto.EducationalLevel
user.Allergies = dto.Allergies
user.MedicalCondition = dto.MedicalCondition
user.InterestedFaculties = make([]model.InterestedFaculty, 0, len(dto.InterestedFaculties))
user.VisitingFaculties = make([]model.VisitingFaculty, 0, len(dto.VisitingFaculties))
user.RegisteredEvents = make([]model.EventRegistration, 0)
Expand Down Expand Up @@ -77,6 +79,8 @@ func UserModelToDTO(mUser *model.User) dto.User {
user.Province = mUser.Province
user.Country = mUser.Country
user.EducationalLevel = mUser.EducationalLevel
user.Allergies = mUser.Allergies
user.MedicalCondition = mUser.MedicalCondition
user.InterestedFaculties = make([]dto.FacultyInfo, 0, len(mUser.InterestedFaculties))
user.RegisteredEvents = make([]dto.Schedule, 0, len(mUser.RegisteredEvents))
user.NewsSources = make([]dto.NewsSource, 0, len(mUser.NewsSourceUsers))
Expand Down
4 changes: 4 additions & 0 deletions internal/dto/auth.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type RegisterRequestDTO struct {
Country string `example:"Japan" json:"country"`
Province string `example:"Tokyo" json:"province"`
EducationalLevel string `example:"Ph.D." json:"educational_level"`
Allergies string `example:"Dog" json:"allergies"`
MedicalCondition string `example:"Dog" json:"medical_condition"`
InterestedFaculties []FacultyInfoId `json:"interested_faculties"`
VisitingFaculties []FacultyInfoId `json:"visiting_faculties"`
NewsSource []NewsSource `example:"facebook,instagram,faculty,chula-student,friend,parent,school,other" json:"news_sources"`
Expand Down Expand Up @@ -44,6 +46,8 @@ type User struct {
Province string `example:"Austin" json:"province"`
DesiredRound string `json:"desired_round"`
EducationalLevel string `example:"Ph.D." json:"educational_level"`
Allergies string `example:"Dog" json:"allergies"`
MedicalCondition string `example:"Dog" json:"medical_condition"`
NewsSources []NewsSource `example:"facebook,instagram" json:"news_sources"`
InterestedFaculties []FacultyInfo `json:"interested_faculties"`
RegisteredEvents []Schedule `json:"registered_events"`
Expand Down
2 changes: 1 addition & 1 deletion internal/dto/evtreg.dto.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package dto

type EventRegistrationDTO struct {
ScheduleId int `json:"schedule_id"`
NewsSource []NewsSource `example:"facebook,instagram,faculty,chula-student,friend,parent,school,other" json:"news_sources"`
}
10 changes: 9 additions & 1 deletion internal/evtreg/evtreg.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/isd-sgcu/oph66-backend/apperror"
"github.com/isd-sgcu/oph66-backend/internal/dto"
"github.com/isd-sgcu/oph66-backend/utils"
)

Expand Down Expand Up @@ -33,6 +34,7 @@ type handlerImpl struct {
// @tags event
// @Security Bearer
// @param scheduleId path int true "schedule id"
// @param registerEventDto body dto.EventRegistrationDTO true "Event register body"
// @router /schedules/{scheduleId}/register [post]
func (h *handlerImpl) RegisterEvent(c *gin.Context) {
email := c.GetString("email")
Expand All @@ -41,14 +43,20 @@ func (h *handlerImpl) RegisterEvent(c *gin.Context) {
return
}

var body dto.EventRegistrationDTO
if err := c.ShouldBindJSON(&body); err != nil {
utils.ReturnError(c, apperror.BadRequest)
return
}

scheduleIdstr := c.Param("scheduleId")
scheduleId, err := strconv.Atoi(scheduleIdstr)
if err != nil {
utils.ReturnError(c, apperror.BadRequest)
return
}

if apperr := h.svc.RegisterEvent(c, email, scheduleId); apperr != nil {
if apperr := h.svc.RegisterEvent(c, email, scheduleId, &body); apperr != nil {
utils.ReturnError(c, apperr)
return
}
Expand Down
12 changes: 4 additions & 8 deletions internal/evtreg/evtreg.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Repository interface {
GetUserWithEventRegistrationByEmail(user *model.User, email string) error
GetScheduleById(schedule *model.Schedule, scheduleId int) error
RegisterEvent(userId int, scheduleId int) error
RegisterEvent(evtreg *model.EventRegistration) error
}

func NewRepository(db *gorm.DB) Repository {
Expand All @@ -31,13 +31,13 @@ func (r *repositoryImpl) GetScheduleById(schedule *model.Schedule, scheduleId in
return r.db.Model(schedule).Where("id = ?", scheduleId).First(schedule).Error
}

func (r *repositoryImpl) RegisterEvent(userId int, scheduleId int) error {
func (r *repositoryImpl) RegisterEvent(evtreg *model.EventRegistration) error {
return r.db.Transaction(func(tx *gorm.DB) error {
var schedule model.Schedule

if err := tx.Clauses(clause.Locking{
Strength: "UPDATE",
}).Model(&schedule).Preload("Event").Where("id = ?", scheduleId).Find(&schedule).Error; err != nil {
}).Model(&schedule).Preload("Event").Where("id = ?", evtreg.ScheduleId).Find(&schedule).Error; err != nil {
return err
}

Expand All @@ -48,11 +48,7 @@ func (r *repositoryImpl) RegisterEvent(userId int, scheduleId int) error {
schedule.CurrentAttendee++
tx.Save(&schedule)

var reg model.EventRegistration
reg.ScheduleId = scheduleId
reg.UserId = userId

if err := tx.Create(&reg).Error; err != nil {
if err := tx.Create(&evtreg).Error; err != nil {
return err
}

Expand Down
Loading

0 comments on commit 7d4c145

Please sign in to comment.