Skip to content

Commit

Permalink
Merge branch 'feature/training-record'
Browse files Browse the repository at this point in the history
  • Loading branch information
masashiy22 committed May 13, 2024
2 parents a8ec4be + 59ecdad commit 8c60dc1
Show file tree
Hide file tree
Showing 6 changed files with 527 additions and 0 deletions.
6 changes: 6 additions & 0 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func RegisterRoutes(r *gin.Engine) {
r.POST("/training-items", CreateTraningItem)
r.PUT("/training-items/:training-item-id", UpdateTraningItem)
r.DELETE("/training-items/:training-item-id", DeleteTraningItem)

r.GET("/training-records", ListTrainingRecords)
r.GET("/training-records/:training-record-id", GetTrainingRecord)
r.POST("/training-records", CreateTrainingRecord)
r.PUT("/training-records/:training-record-id", UpdateTrainingRecord)
r.DELETE("/training-records/:training-record-id", DeleteTrainingRecord)
}

func SetCors(r *gin.Engine) {
Expand Down
150 changes: 150 additions & 0 deletions controller/training_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package controller

import (
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"github.com/openkrafter/anytore-backend/customerror"
"github.com/openkrafter/anytore-backend/logger"
"github.com/openkrafter/anytore-backend/model"
"github.com/openkrafter/anytore-backend/service"
)

func ListTrainingRecords(c *gin.Context) {
userId, err := ValidateTokenAndGetUserId(c)
if err != nil {
logger.Logger.Error("Token Error.", logger.ErrAttr(err))
c.JSON(http.StatusForbidden, gin.H{"error": "You are not authenticated"})
return
}

ctx := c.Request.Context()
trainingRecords, err := service.GetTrainingRecords(ctx, userId)
if err != nil {
logger.Logger.Error("ListTrainingRecords Failed.", logger.ErrAttr(err))
return
}
if trainingRecords == nil {
error404 := customerror.NewError404()
c.JSON(error404.ErrorCode, error404.Body)
return
}

c.JSON(http.StatusOK, trainingRecords)
}

func GetTrainingRecord(c *gin.Context) {
userId, err := ValidateTokenAndGetUserId(c)
if err != nil {
logger.Logger.Error("Token Error.", logger.ErrAttr(err))
c.JSON(http.StatusForbidden, gin.H{"error": "You are not authenticated"})
return
}

trainingRecordId, err := strconv.Atoi(c.Param("training-record-id"))
if err != nil {
logger.Logger.Error("GetTrainingRecord Failed.", logger.ErrAttr(err))
return
}

ctx := c.Request.Context()
trainingRecord, err := service.GetTrainingRecord(ctx, trainingRecordId, userId)
if err != nil {
logger.Logger.Error("GetTrainingRecord Failed.", logger.ErrAttr(err))
return
}

c.JSON(http.StatusOK, trainingRecord)
}

func CreateTrainingRecord(c *gin.Context) {
userId, err := ValidateTokenAndGetUserId(c)
if err != nil {
logger.Logger.Error("Token Error.", logger.ErrAttr(err))
c.JSON(http.StatusForbidden, gin.H{"error": "You are not authenticated"})
return
}

trainingRecord := new(model.TrainingRecord)
if err := c.BindJSON(trainingRecord); err != nil {
logger.Logger.Error("CreateTrainingRecord Failed.", logger.ErrAttr(err))
return
}

trainingRecord.UserId = userId

ctx := c.Request.Context()
err = service.CreateTrainingRecord(ctx, trainingRecord)
if err != nil {
logger.Logger.Error("CreateTrainingRecord Failed.", logger.ErrAttr(err))
return
}

c.JSON(http.StatusCreated, trainingRecord)
}

func UpdateTrainingRecord(c *gin.Context) {
userId, err := ValidateTokenAndGetUserId(c)
if err != nil {
logger.Logger.Error("Token Error.", logger.ErrAttr(err))
c.JSON(http.StatusForbidden, gin.H{"error": "You are not authenticated"})
return
}

trainingRecord := new(model.TrainingRecord)
if err := c.BindJSON(trainingRecord); err != nil {
logger.Logger.Error("UpdateTrainingRecord Failed.", logger.ErrAttr(err))
return
}

trainingRecord.UserId = userId
trainingRecord.Id, err = strconv.Atoi(c.Param("training-record-id"))
if err != nil {
logger.Logger.Error("UpdateTrainingRecord Failed.", logger.ErrAttr(err))
return
}

ctx := c.Request.Context()
err = service.UpdateTrainingRecord(ctx, trainingRecord, userId)
if err != nil {
if customErr, ok := err.(*customerror.Error404); ok {
c.JSON(customErr.ErrorCode, customErr.Body)
logger.Logger.Error("UpdateTraningRecord 404.", logger.ErrAttr(err))
} else {
logger.Logger.Error("UpdateTraningRecord Failed.", logger.ErrAttr(err))
}
return
}

c.JSON(http.StatusCreated, trainingRecord)
}

func DeleteTrainingRecord(c *gin.Context) {
userId, err := ValidateTokenAndGetUserId(c)
if err != nil {
logger.Logger.Error("Token Error.", logger.ErrAttr(err))
c.JSON(http.StatusForbidden, gin.H{"error": "You are not authenticated"})
return
}

trainingRecordId, err := strconv.Atoi(c.Param("training-record-id"))
if err != nil {
logger.Logger.Error("DeleteTrainingRecord Failed.", logger.ErrAttr(err))
return
}

ctx := c.Request.Context()
err = service.DeleteTrainingRecord(ctx, trainingRecordId, userId)
if err != nil {
if customErr, ok := err.(*customerror.Error404); ok {
c.JSON(customErr.ErrorCode, customErr.Body)
logger.Logger.Error("DeleteTrainingRecord 404.", logger.ErrAttr(err))
} else {
logger.Logger.Error("DeleteTrainingRecord Failed.", logger.ErrAttr(err))
}
return
}

c.JSON(http.StatusNoContent, gin.H{"message": "TrainingRecord deleted successfully."})
}
9 changes: 9 additions & 0 deletions model/training_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package model

type TrainingRecord struct {
Id int `json:"id"`
UserId int `json:"userId"`
TrainingItemId int `json:"trainingItemId"`
Record float64 `json:"record"`
Date int64 `json:"date"`
}
37 changes: 37 additions & 0 deletions platform/local/scripts/dynamodb-local-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,40 @@ aws dynamodb create-table \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--table-class "STANDARD" \
--no-deletion-protection-enabled


aws dynamodb create-table \
--no-cli-pager \
--endpoint-url http://localhost:8000 \
--table-name TrainingRecord \
--attribute-definitions AttributeName=Id,AttributeType=N AttributeName=UserId,AttributeType=N \
--key-schema AttributeName=Id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--table-class "STANDARD" \
--no-deletion-protection-enabled \
--global-secondary-indexes \
'[
{
"IndexName": "UserIdIndex",
"KeySchema": [
{"AttributeName": "UserId", "KeyType": "HASH"}
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
}
}
]'

aws dynamodb create-table \
--no-cli-pager \
--endpoint-url http://localhost:8000 \
--table-name TrainingRecordCounter \
--attribute-definitions AttributeName=CountKey,AttributeType=S \
--key-schema AttributeName=CountKey,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--table-class "STANDARD" \
--no-deletion-protection-enabled
10 changes: 10 additions & 0 deletions platform/local/scripts/dynamodb-local-teardown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ aws dynamodb delete-table \
--no-cli-pager \
--endpoint-url http://localhost:8000 \
--table-name TrainingItemCounter

aws dynamodb delete-table \
--no-cli-pager \
--endpoint-url http://localhost:8000 \
--table-name TrainingRecord

aws dynamodb delete-table \
--no-cli-pager \
--endpoint-url http://localhost:8000 \
--table-name TrainingRecordCounter
Loading

0 comments on commit 8c60dc1

Please sign in to comment.