Skip to content

Commit

Permalink
Merge pull request #12 from gdsc-ncku/Fix_small_issue
Browse files Browse the repository at this point in the history
Reframe comments and fix bug
  • Loading branch information
vax-r authored Feb 2, 2024
2 parents 531fe16 + 6cc2008 commit 21577d4
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 65 deletions.
14 changes: 7 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ func ReverseProxy() gin.HandlerFunc {
originalURL := *c.Request.URL

// Set the scheme and host
req.URL.Scheme = "http" // or "https" if your target is using SSL
// Set req.URL.Scheme as "http" or "https" based on the protocol your target is using
req.URL.Scheme = "http"
req.URL.Host = c.Request.Host

// if the suffix is /docs, then we need to change it to /swagger/index.html
// otherwise, we need to substitute /docs with /swagger
// If the suffix is '/docs', then we need to change it to '/swagger/index.html'
// Otherwise, we need to substitute '/docs' with '/swagger'
if originalURL.Path == "/docs/" {
req.URL.Path = "/swagger/index.html"
} else {
Expand Down Expand Up @@ -64,10 +65,10 @@ func SetUpAsynqMon(app *bootstrap.Application) {
}

func main() {
// init config
// Init config
app := bootstrap.App()

// init services
// Init services
userService := service.NewUserService(app.Conn, app.Cache)
eventService := service.NewEventService(app.Conn, app.Cache)
asynqService := service.NewAsynqService(app.AsynqClient, app.AsyncqInspector, app.Env)
Expand All @@ -78,7 +79,7 @@ func main() {
AsynqService: asynqService,
}

// init routes
// Init routes
router.RegisterRoutes(app, services)

// setup swagger
Expand All @@ -95,6 +96,5 @@ func main() {
)
app.Engine.GET("/docs/*any", ReverseProxy())

// run app
app.Run()
}
4 changes: 2 additions & 2 deletions pkg/bootstrap/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func App(opts ...AppOpts) *Application {
return app
}

// Run run the application
// Run the application
func (app *Application) Run() {
srv := &http.Server{
Addr: fmt.Sprintf(":%d", app.Env.Server.Port),
Expand All @@ -86,7 +86,7 @@ func (app *Application) Run() {
log.Fatalf("Error starting server: %v", err)

case <-shutdown:
log.Println("shutting down...")
log.Println("Shutting down the server...")

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion pkg/bootstrap/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (env *DBEnv) Dialect(kind string) gorm.Dialector {
dsn := fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", env.Username, env.Password, env.Host, env.Port, env.Database)
return sqlserver.Open(dsn)
default:
panic("unsupported database kind")
panic("Unsupported database kind")
}
}

Expand Down
7 changes: 3 additions & 4 deletions pkg/controller/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func RetrievePagination(c *gin.Context) (page, limit uint64) {

limitStr := c.Query("limit")

// convert string to uint64
// Convert string to uint64
page, err := strconv.ParseUint(pageStr, 10, 64)
if err != nil {
page = 1
Expand All @@ -27,14 +27,13 @@ func RetrievePagination(c *gin.Context) (page, limit uint64) {
}

// RetrieveIdentity retrieves the identity of the user from the context.
// raise: if true, raise a http error if the identity does not exist
// raise: Raise a http error when the identity doesn't exist.
func RetrieveIdentity(c *gin.Context, raise bool) (identity *model.Identity, exist bool) {
id, exist := c.Get("identity")
if !exist {
if raise {
// raise not login error
c.AbortWithStatusJSON(401, model.Response{
Msg: "not login",
Msg: "Login Required",
})
}
return nil, false
Expand Down
39 changes: 20 additions & 19 deletions pkg/controller/event_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bikefest/pkg/model"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
)

type EventController struct {
Expand Down Expand Up @@ -39,13 +40,13 @@ func (ctrl *EventController) GetEventByID(c *gin.Context) {
id := c.Param("id")
event, err := ctrl.eventService.FindByID(c, id)
if err != nil {
c.AbortWithStatusJSON(404, model.Response{
c.AbortWithStatusJSON(http.StatusNotFound, model.Response{
Msg: err.Error(),
})
return
}

c.JSON(200, model.EventResponse{
c.JSON(http.StatusOK, model.EventResponse{
Data: event,
})
}
Expand All @@ -65,12 +66,12 @@ func (ctrl *EventController) GetAllEvent(c *gin.Context) {
page, limit := RetrievePagination(c)
events, err := ctrl.eventService.FindAll(c, int64(page), int64(limit))
if err != nil {
c.AbortWithStatusJSON(500, model.Response{
c.AbortWithStatusJSON(http.StatusInternalServerError, model.Response{
Msg: err.Error(),
})
}

c.JSON(200, model.EventListResponse{
c.JSON(http.StatusOK, model.EventListResponse{
Data: events,
})
}
Expand All @@ -94,22 +95,22 @@ func (ctrl *EventController) UpdateEvent(c *gin.Context) {
id := c.Param("id")
identity, _ := RetrieveIdentity(c, true)
if identity.UserID != "admin" {
c.AbortWithStatusJSON(403, model.Response{
Msg: "permission denied",
c.AbortWithStatusJSON(http.StatusForbidden, model.Response{
Msg: "Permission denied",
})
return
}
_ = identity.UserID
// _ = identity.UserID
var request model.CreateEventRequest
if err := c.ShouldBind(&request); err != nil {
c.AbortWithStatusJSON(400, model.Response{
c.AbortWithStatusJSON(http.StatusBadRequest, model.Response{
Msg: err.Error(),
})
return
}
event, err := ctrl.eventService.FindByID(c, id)
if err != nil {
c.AbortWithStatusJSON(500, model.Response{
c.AbortWithStatusJSON(http.StatusInternalServerError, model.Response{
Msg: err.Error(),
})
return
Expand All @@ -124,13 +125,13 @@ func (ctrl *EventController) UpdateEvent(c *gin.Context) {
}
_, err = ctrl.eventService.Update(c, updatedEvent)
if err != nil {
c.AbortWithStatusJSON(500, model.Response{
c.AbortWithStatusJSON(http.StatusInternalServerError, model.Response{
Msg: err.Error(),
})
return
}

c.JSON(200, model.EventResponse{
c.JSON(http.StatusOK, model.EventResponse{
Data: event,
})
}
Expand All @@ -148,26 +149,26 @@ func (ctrl *EventController) StoreAllEvent(c *gin.Context) {

resp, err := http.Get(jsonURL)
if err != nil {
c.AbortWithStatusJSON(500, model.Response{
c.AbortWithStatusJSON(http.StatusInternalServerError, model.Response{
Msg: err.Error(),
})
return
}
defer resp.Body.Close()

if err := json.NewDecoder(resp.Body).Decode(&eventDetails); err != nil {
c.AbortWithStatusJSON(500, model.Response{
c.AbortWithStatusJSON(http.StatusInternalServerError, model.Response{
Msg: err.Error(),
})
return
}
for _, eventDetail := range eventDetails {
eventStartTimeP := parseEventTime("2024/"+eventDetail.Date+" "+eventDetail.StartTime, model.EventTimeLayout)
eventEndTimeP := parseEventTime("2024/"+eventDetail.Date+" "+eventDetail.EndTime, model.EventTimeLayout)
// stringfy the event eventDetail
// Stringnify the event eventDetail
eventDetailJson, err := json.Marshal(eventDetail)
if err != nil {
c.AbortWithStatusJSON(500, model.Response{
c.AbortWithStatusJSON(http.StatusInternalServerError, model.Response{
Msg: err.Error(),
})
return
Expand All @@ -184,13 +185,13 @@ func (ctrl *EventController) StoreAllEvent(c *gin.Context) {
}
err = ctrl.eventService.StoreAll(c, events)
if err != nil {
c.AbortWithStatusJSON(500, model.Response{
c.AbortWithStatusJSON(http.StatusInternalServerError, model.Response{
Msg: err.Error(),
})
return
}
c.JSON(200, model.Response{
Msg: "store all events success",
c.JSON(http.StatusOK, model.Response{
Msg: "Store all events success",
})
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/oauth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"bikefest/pkg/line_utils"
"bikefest/pkg/model"
"fmt"
"github.com/gin-gonic/gin"
social "github.com/kkdai/line-login-sdk-go"
"log"
"net/http"
"strings"

"github.com/gin-gonic/gin"
social "github.com/kkdai/line-login-sdk-go"
)

func NewOAuthController(lineSocialClient *social.Client, env *bootstrap.Env, userSvc model.UserService) *OAuthController {
Expand Down Expand Up @@ -49,7 +50,7 @@ func (ctrl *OAuthController) LineLogin(c *gin.Context) {
state := originalUrl + "$" + social.GenerateNonce()
if len(state) == 0 {
c.AbortWithStatusJSON(http.StatusBadRequest, model.Response{
Msg: "login with the wrong way, please try again in official website",
Msg: "Login with the wrong way, please try again in official website",
})
return
}
Expand Down Expand Up @@ -78,7 +79,7 @@ func (ctrl *OAuthController) LineLoginCallback(c *gin.Context) {
stateInCookie, err := c.Cookie("state")
if err != nil || stateInCookie != state {
c.AbortWithStatusJSON(http.StatusBadRequest, model.Response{
Msg: "state cookie is invalid",
Msg: "State cookie is invalid",
})
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ func NewPsychoTestController(db *gorm.DB) *PsychoTestController {
return &PsychoTestController{db: db}
}

// CreateType create new psychological type
// Function: CreateType
// The function is responsible for creating
// new psychological type
func (controller *PsychoTestController) CreateType(context *gin.Context) {
newType := context.Query("type")

Expand All @@ -34,14 +36,12 @@ func (controller *PsychoTestController) CreateType(context *gin.Context) {
Count: 0,
}

result := controller.db.Create(&record)

if result.Error != nil {
if result := controller.db.Create(&record); result.Error != nil {
context.JSON(http.StatusBadRequest, gin.H{
"status": "Failed",
"message": result.Error,
})
panic(result.Error)
return
}

context.JSON(http.StatusOK, gin.H{
Expand All @@ -50,7 +50,9 @@ func (controller *PsychoTestController) CreateType(context *gin.Context) {
})
}

// TypeAddCount Add the count of selected psychological type
// Function: TypeAddCount
// The function is responsible for adding
// the count of selected psychological type
func (controller *PsychoTestController) TypeAddCount(context *gin.Context) {
testType := context.PostForm("type")
count, _ := strconv.Atoi(context.PostForm("count"))
Expand Down Expand Up @@ -83,7 +85,9 @@ func (controller *PsychoTestController) TypeAddCount(context *gin.Context) {
})
}

// CountTypePercentage retrieve the percentage of each type
// Function: CountTypePercentage
// The function is responsible for retreval of
// of the percentage of each type
func (controller *PsychoTestController) CountTypePercentage(context *gin.Context) {
var queryTypes []*model.PsychoTest

Expand All @@ -107,7 +111,7 @@ func (controller *PsychoTestController) CountTypePercentage(context *gin.Context
if sum == 0 {
context.JSON(http.StatusNotFound, gin.H{
"status": "Failed",
"message": "No tested data",
"message": "No test data",
})
}

Expand Down
Loading

0 comments on commit 21577d4

Please sign in to comment.