From 013847218192e07595448674820f8a50dade8b4a Mon Sep 17 00:00:00 2001 From: melodyyu754 <118621363+melodyyu754@users.noreply.github.com> Date: Wed, 4 Oct 2023 15:34:19 -0400 Subject: [PATCH 1/2] wrote/updated CRUD for various objects --- server/src/controller/progress.go | 86 +++++++++++++++++++++++++++ server/src/controller/subtask.go | 86 +++++++++++++++++++++++++++ server/src/controller/task.go | 55 ++++++++++++++++- server/src/controller/user_profile.go | 86 +++++++++++++++++++++++++++ 4 files changed, 312 insertions(+), 1 deletion(-) create mode 100644 server/src/controller/progress.go create mode 100644 server/src/controller/subtask.go create mode 100644 server/src/controller/user_profile.go diff --git a/server/src/controller/progress.go b/server/src/controller/progress.go new file mode 100644 index 0000000..6373b08 --- /dev/null +++ b/server/src/controller/progress.go @@ -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") +} diff --git a/server/src/controller/subtask.go b/server/src/controller/subtask.go new file mode 100644 index 0000000..9b1fc1e --- /dev/null +++ b/server/src/controller/subtask.go @@ -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") +} diff --git a/server/src/controller/task.go b/server/src/controller/task.go index d4e817a..ba64d67 100644 --- a/server/src/controller/task.go +++ b/server/src/controller/task.go @@ -4,6 +4,7 @@ import ( "net/http" "server/src/model" + "github.com/go-playground/validator" "github.com/labstack/echo/v4" "gorm.io/gorm" ) @@ -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) @@ -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") +} diff --git a/server/src/controller/user_profile.go b/server/src/controller/user_profile.go new file mode 100644 index 0000000..a93723c --- /dev/null +++ b/server/src/controller/user_profile.go @@ -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 UserProfileController struct { + DB *gorm.DB +} + +func (up *UserProfileController) GetAllUserProfiles(c echo.Context) error { + var userprofiles []model.UserProfile + + up.DB.Find(&userprofiles) + return c.JSON(http.StatusOK, userprofiles) +} + +func (up *UserProfileController) GetUserProfile(c echo.Context) error { + var userprofile model.UserProfile + userprofileID := c.Param("id") + + up.DB.First(&userprofile, userprofileID) + + if userprofile.ID == 0 { + return c.JSON(http.StatusNotFound, "User Profile not found") + } + + return c.JSON(http.StatusOK, userprofile) +} + +func (up *UserProfileController) CreateUserProfile(c echo.Context) error { + var userprofile model.UserProfile + + if err := c.Bind(&userprofile); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) + } + + validator := validator.New() + + if err := validator.Struct(userprofile); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) + } + + up.DB.Create(&userprofile) + + return c.JSON(http.StatusCreated, userprofile) +} + +func (up *UserProfileController) UpdateUserProfile(c echo.Context) error { + var userprofile model.UserProfile + userprofileID := c.Param("id") + + up.DB.First(&userprofile, userprofileID) + + if userprofile.ID == 0 { + return c.JSON(http.StatusNotFound, "User Profile not found") + } + + if err := c.Bind(&userprofile); err != nil { + return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()}) + } + + up.DB.Save(&userprofile) + + return c.JSON(http.StatusOK, userprofile) +} + +func (up *UserProfileController) DeleteUserProfile(c echo.Context) error { + var userprofile model.UserProfile + userprofileID := c.Param("id") + + up.DB.First(&userprofile, userprofileID) + + if userprofile.ID == 0 { + return c.JSON(http.StatusNotFound, "User Profile not found") + } + + up.DB.Delete(&userprofile) + + return c.JSON(http.StatusOK, "User Profile deleted") +} From 594dfc4bbcec7ed360ae2130542c2678ea74a933 Mon Sep 17 00:00:00 2001 From: Ania Misiorek Date: Wed, 4 Oct 2023 19:10:53 -0400 Subject: [PATCH 2/2] adding routes --- client/package.json | 2 +- client/yarn.lock | 8 ++++---- server/src/controller/setup.go | 36 ++++++++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/client/package.json b/client/package.json index 5dfebc2..6c99db0 100644 --- a/client/package.json +++ b/client/package.json @@ -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", diff --git a/client/yarn.lock b/client/yarn.lock index cfaecf5..d1825d9 100644 --- a/client/yarn.lock +++ b/client/yarn.lock @@ -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" "*" diff --git a/server/src/controller/setup.go b/server/src/controller/setup.go index b4da6f9..033dbc2 100644 --- a/server/src/controller/setup.go +++ b/server/src/controller/setup.go @@ -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) @@ -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) }