Skip to content

Commit

Permalink
Merge pull request #4 from GenerateNU/feature/database-design
Browse files Browse the repository at this point in the history
Feature/database design
  • Loading branch information
DOOduneye authored Oct 5, 2023
2 parents 51cd7a7 + 594dfc4 commit 5ec30ca
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 8 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@babel/core": "^7.20.0",
"@babel/preset-env": "^7.1.6",
"@react-native-community/eslint-config": "^3.2.0",
"@types/react": "~18.0.14",
"@types/react": "~18.2.14",
"@types/react-native": "^0.72.2",
"babel-plugin-module-resolver": "^5.0.0",
"eslint": "^8.48.0",
Expand Down
8 changes: 4 additions & 4 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2345,10 +2345,10 @@
"@types/scheduler" "*"
csstype "^3.0.2"

"@types/react@~18.0.14":
version "18.0.38"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.0.38.tgz#02a23bef8848b360a0d1dceef4432c15c21c600c"
integrity sha512-ExsidLLSzYj4cvaQjGnQCk4HFfVT9+EZ9XZsQ8Hsrcn8QNgXtpZ3m9vSIC2MWtx7jHictK6wYhQgGh6ic58oOw==
"@types/react@~18.2.14":
version "18.2.22"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.2.22.tgz#abe778a1c95a07fa70df40a52d7300a40b949ccb"
integrity sha512-60fLTOLqzarLED2O3UQImc/lsNRgG0jE/a1mPW9KjMemY0LMITWEsbS4VvZ4p6rorEHd5YKxxmMKSDK505GHpA==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
Expand Down
86 changes: 86 additions & 0 deletions server/src/controller/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package controller

import (
"net/http"
"server/src/model"

"github.com/go-playground/validator"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)

type ProgressController struct {
DB *gorm.DB
}

func (p *ProgressController) GetAllProgress(c echo.Context) error {
var progress []model.Progress

p.DB.Find(&progress)
return c.JSON(http.StatusOK, progress)
}

func (p *ProgressController) GetProgress(c echo.Context) error {
var progress model.Progress
progressID := c.Param("id")

p.DB.First(&progress, progressID)

if progress.ID == 0 {
return c.JSON(http.StatusNotFound, "Progress not found")
}

return c.JSON(http.StatusOK, progress)
}

func (p *ProgressController) CreateProgress(c echo.Context) error {
var progress model.Progress

if err := c.Bind(&progress); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}

validator := validator.New()

if err := validator.Struct(progress); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}

p.DB.Create(&progress)

return c.JSON(http.StatusCreated, progress)
}

func (p *ProgressController) UpdateProgress(c echo.Context) error {
var progress model.Progress
progressID := c.Param("id")

p.DB.First(&progress, progressID)

if progress.ID == 0 {
return c.JSON(http.StatusNotFound, "Progress not found")
}

if err := c.Bind(&progress); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}

p.DB.Save(&progress)

return c.JSON(http.StatusOK, progress)
}

func (p *ProgressController) DeleteProgress(c echo.Context) error {
var progress model.Progress
progressID := c.Param("id")

p.DB.First(&progress, progressID)

if progress.ID == 0 {
return c.JSON(http.StatusNotFound, "Progress not found")
}

p.DB.Delete(&progress)

return c.JSON(http.StatusOK, "Progress deleted")
}
36 changes: 34 additions & 2 deletions server/src/controller/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ func SetupControllers(e *echo.Echo, db *gorm.DB) {
// Create a new instance of the controller with the database connection
userController := UserController{db}
personaController := PersonaController{db}
awsController := AwsController{db}
userProfileController := UserProfileController{db}
taskController := TaskController{db}
subtaskController := SubtaskController{db}
progressController := ProgressController{db}
awsController := AwsController{db}

// User routes
e.GET("api/users", userController.GetAllUsers)
Expand All @@ -25,7 +29,35 @@ func SetupControllers(e *echo.Echo, db *gorm.DB) {
e.PUT("api/personas/:id", personaController.UpdatePersona)
e.DELETE("api/personas/:id", personaController.DeletePersona)

// AWS
// User Profile routes
e.GET("api/profile", userProfileController.GetAllUserProfiles)
e.GET("api/profile/:id", userProfileController.GetUserProfile)
e.POST("api/profile", userProfileController.CreateUserProfile)
e.PUT("api/profile/:id", userProfileController.UpdateUserProfile)
e.DELETE("api/profile/:id", userProfileController.DeleteUserProfile)

// Task routes
e.GET("api/task", taskController.GetAllTasks)
e.GET("api/task/:id", taskController.GetTask)
e.POST("api/task", taskController.CreateTask)
e.PUT("api/task/:id", taskController.UpdateTask)
e.DELETE("api/task/:id", taskController.DeleteTask)

// Task routes
e.GET("api/subtask", subtaskController.GetAllSubtasks)
e.GET("api/subtask/:id", subtaskController.GetAllSubtasks)
e.POST("api/subtask", subtaskController.CreateSubtask)
e.PUT("api/subtask/:id", subtaskController.UpdateSubtask)
e.DELETE("api/subtask/:id", subtaskController.DeleteSubtask)

// Progress routes
e.GET("api/progress", progressController.GetAllProgress)
e.GET("api/progress/:id", progressController.GetProgress)
e.POST("api/progress", progressController.CreateProgress)
e.PUT("api/progress/:id", progressController.UpdateProgress)
e.DELETE("api/progress/:id", progressController.DeleteProgress)

// AWS
e.GET("v1/api/aws", awsController.dump)

}
86 changes: 86 additions & 0 deletions server/src/controller/subtask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package controller

import (
"net/http"
"server/src/model"

"github.com/go-playground/validator"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)

type SubtaskController struct {
DB *gorm.DB
}

func (s *SubtaskController) GetAllSubtasks(c echo.Context) error {
var subtasks []model.Subtask

s.DB.Find(&subtasks)
return c.JSON(http.StatusOK, subtasks)
}

func (s *SubtaskController) GetSubtask(c echo.Context) error {
var subtask model.Subtask
subtaskID := c.Param("id")

s.DB.First(&subtask, subtaskID)

if subtask.ID == 0 {
return c.JSON(http.StatusNotFound, "Subtask not found")
}

return c.JSON(http.StatusOK, subtask)
}

func (s *SubtaskController) CreateSubtask(c echo.Context) error {
var subtask model.Subtask

if err := c.Bind(&subtask); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}

validator := validator.New()

if err := validator.Struct(subtask); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}

s.DB.Create(&subtask)

return c.JSON(http.StatusCreated, subtask)
}

func (s *SubtaskController) UpdateSubtask(c echo.Context) error {
var subtask model.Subtask
subtaskID := c.Param("id")

s.DB.First(&subtask, subtaskID)

if subtask.ID == 0 {
return c.JSON(http.StatusNotFound, "Subtask not found")
}

if err := c.Bind(&subtask); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}

s.DB.Save(&subtask)

return c.JSON(http.StatusOK, subtask)
}

func (s *SubtaskController) DeleteSubtask(c echo.Context) error {
var subtask model.Subtask
subtaskID := c.Param("id")

s.DB.First(&subtask, subtaskID)

if subtask.ID == 0 {
return c.JSON(http.StatusNotFound, "Subtask not found")
}

s.DB.Delete(&subtask)

return c.JSON(http.StatusOK, "Subtask deleted")
}
55 changes: 54 additions & 1 deletion server/src/controller/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"server/src/model"

"github.com/go-playground/validator"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
)
Expand All @@ -13,7 +14,7 @@ type TaskController struct {
}

func (t *TaskController) GetAllTasks(c echo.Context) error {
var task []model.User
var task []model.Task

t.DB.Find(&task)
return c.JSON(http.StatusOK, task)
Expand All @@ -31,3 +32,55 @@ func (t *TaskController) GetTask(c echo.Context) error {

return c.JSON(http.StatusOK, task)
}

func (t *TaskController) CreateTask(c echo.Context) error {
var task model.Task

if err := c.Bind(&task); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}

validator := validator.New()

if err := validator.Struct(task); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}

t.DB.Create(&task)

return c.JSON(http.StatusCreated, task)
}

func (t *TaskController) UpdateTask(c echo.Context) error {
var task model.Task
taskID := c.Param("id")

t.DB.First(&task, taskID)

if task.ID == 0 {
return c.JSON(http.StatusNotFound, "Task not found")
}

if err := c.Bind(&task); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}

t.DB.Save(&task)

return c.JSON(http.StatusOK, task)
}

func (t *TaskController) DeleteTask(c echo.Context) error {
var task model.Task
taskID := c.Param("id")

t.DB.First(&task, taskID)

if task.ID == 0 {
return c.JSON(http.StatusNotFound, "Task not found")
}

t.DB.Delete(&task)

return c.JSON(http.StatusOK, "Task deleted")
}
Loading

0 comments on commit 5ec30ca

Please sign in to comment.