Skip to content

Commit

Permalink
refactor: make be routes and transactions private (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattCMcCoy authored Apr 8, 2024
1 parent 52b8de7 commit a6c6050
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 128 deletions.
6 changes: 3 additions & 3 deletions backend/schema/files/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func FileGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {

files := v1.Group("files")
{
files.POST("/upload", c.UploadFile)
files.POST("/upload", c.uploadFile)
}

return files
Expand All @@ -38,7 +38,7 @@ func FileGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {
// @success 200 {object} models.File
// @failure 400 {object} string
// @router /files/upload [post]
func (pg *PgModel) UploadFile(c *gin.Context) {
func (pg *PgModel) uploadFile(c *gin.Context) {
var file models.File

form, err := c.MultipartForm()
Expand Down Expand Up @@ -66,7 +66,7 @@ func (pg *PgModel) UploadFile(c *gin.Context) {

defer fileData.Close()

err = UploadFile(pg.Conn, file, fileResponse, fileData)
err = uploadFile(pg.Conn, file, fileResponse, fileData)
if err != nil {
c.JSON(http.StatusBadRequest, "Failed to create file: "+err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion backend/schema/files/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

var AWS_BUCKET_NAME = "care-wallet-storage"

func UploadFile(pool *pgxpool.Pool, file models.File, data *multipart.FileHeader, reader io.Reader) error {
func uploadFile(pool *pgxpool.Pool, file models.File, data *multipart.FileHeader, reader io.Reader) error {
file.FileName = data.Filename
file.UploadDate = time.Now().Format("2006-01-02 15:04:05")

Expand Down
38 changes: 15 additions & 23 deletions backend/schema/group-roles/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,18 @@ type PgModel struct {
func GroupRolesGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {
groupRoles := v1.Group("")
{
groupRoles.GET("/:groupId/roles", c.GetGroupRoles)
groupRoles.GET("/:groupId/roles", c.getGroupRoles)

group := v1.Group(":groupId")
{
user := group.Group(":uid")
{
user.DELETE("", c.RemoveUserFromGroup)
user.PATCH(":role", c.ChangeUserGroupRole)
user.PUT(":role", c.AddUserToGroup)
user.DELETE("", c.removeUserFromGroup)
user.PATCH(":role", c.changeUserGroupRole)
user.PUT(":role", c.addUserToGroup)
}
}

member := v1.Group("member")
{
user := member.Group(":uid")
{
user.GET("", c.GetGroupByUID)
}
}

groupRoles.GET("/member/:uid", c.getGroupByUID)
}

return groupRoles
Expand All @@ -53,7 +45,7 @@ func GroupRolesGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {
// @success 200 {object} string
// @failure 400 {object} string
// @router /group/{groupId}/{uid}/{role} [patch]
func (pg *PgModel) ChangeUserGroupRole(c *gin.Context) {
func (pg *PgModel) changeUserGroupRole(c *gin.Context) {
gid := c.Param("groupId")
gidInt, err := strconv.Atoi(gid)
uid := c.Param("uid")
Expand All @@ -64,7 +56,7 @@ func (pg *PgModel) ChangeUserGroupRole(c *gin.Context) {
return
}

err = ChangeUserGroupRoleInDB(pg.Conn, gidInt, uid, role)
err = changeUserGroupRoleInDB(pg.Conn, gidInt, uid, role)

if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
Expand All @@ -87,7 +79,7 @@ func (pg *PgModel) ChangeUserGroupRole(c *gin.Context) {
// @success 200 {object} models.GroupRole
// @failure 400 {object} string
// @router /group/{groupId}/{uid}/{role} [put]
func (pg *PgModel) AddUserToGroup(c *gin.Context) {
func (pg *PgModel) addUserToGroup(c *gin.Context) {
gid := c.Param("groupId")
gidInt, err := strconv.Atoi(gid)
uid := c.Param("uid")
Expand All @@ -98,7 +90,7 @@ func (pg *PgModel) AddUserToGroup(c *gin.Context) {
return
}

groupRole, err := AddUserToGroupInDB(pg.Conn, gidInt, uid, role)
groupRole, err := addUserToGroupInDB(pg.Conn, gidInt, uid, role)

if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
Expand All @@ -120,7 +112,7 @@ func (pg *PgModel) AddUserToGroup(c *gin.Context) {
// @success 200 {object} models.GroupRole
// @failure 400 {object} string
// @router /group/{groupId}/{uid} [delete]
func (pg *PgModel) RemoveUserFromGroup(c *gin.Context) {
func (pg *PgModel) removeUserFromGroup(c *gin.Context) {
gid := c.Param("groupId")
gidInt, err := strconv.Atoi(gid)
uid := c.Param("uid")
Expand All @@ -130,7 +122,7 @@ func (pg *PgModel) RemoveUserFromGroup(c *gin.Context) {
return
}

err = RemoveUserFromGroupInDB(pg.Conn, gidInt, uid)
err = removeUserFromGroupInDB(pg.Conn, gidInt, uid)

if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
Expand All @@ -151,9 +143,9 @@ func (pg *PgModel) RemoveUserFromGroup(c *gin.Context) {
// @success 200 {object} string
// @failure 400 {object} string
// @router /group/member/{uid} [get]
func (pg *PgModel) GetGroupByUID(c *gin.Context) {
func (pg *PgModel) getGroupByUID(c *gin.Context) {
uid := c.Param("uid")
groupRole, err := GetGroupMemberByUIDFromDB(pg.Conn, uid)
groupRole, err := getGroupMemberByUIDFromDB(pg.Conn, uid)

if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
Expand All @@ -173,7 +165,7 @@ func (pg *PgModel) GetGroupByUID(c *gin.Context) {
//
// @success 200 {array} models.GroupRole
// @router /group/{groupId}/roles [get]
func (pg *PgModel) GetGroupRoles(c *gin.Context) {
func (pg *PgModel) getGroupRoles(c *gin.Context) {
gid := c.Param("groupId")
gidInt, err := strconv.Atoi(gid)

Expand All @@ -182,7 +174,7 @@ func (pg *PgModel) GetGroupRoles(c *gin.Context) {
return
}

careGroups, err := GetAllGroupRolesFromDB(pg.Conn, gidInt)
careGroups, err := getAllGroupRolesFromDB(pg.Conn, gidInt)

if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
Expand Down
18 changes: 11 additions & 7 deletions backend/schema/group-roles/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
)

func ChangeUserGroupRoleInDB(pool *pgxpool.Pool, gid int, uid string, role string) error {
fmt.Println(role)
func changeUserGroupRoleInDB(pool *pgxpool.Pool, gid int, uid string, role string) error {
_, err := pool.Exec(context.Background(), "UPDATE group_roles SET role = $1 WHERE group_id = $2 AND user_id = $3", role, gid, uid)

return err
if err != nil {
fmt.Printf("Error changing group role: %v", err)
return err
}

return nil
}

func AddUserToGroupInDB(pool *pgxpool.Pool, gid int, uid string, role string) (models.GroupRole, error) {
func addUserToGroupInDB(pool *pgxpool.Pool, gid int, uid string, role string) (models.GroupRole, error) {
var groupMember models.GroupRole
err := pool.QueryRow(context.Background(), "INSERT into group_roles (group_id, user_id, role) VALUES ($1, $2, $3) RETURNING *", gid, uid, role).Scan(&groupMember.GroupID, &groupMember.UserID, &groupMember.Role)

Expand All @@ -27,7 +31,7 @@ func AddUserToGroupInDB(pool *pgxpool.Pool, gid int, uid string, role string) (m
return groupMember, nil
}

func RemoveUserFromGroupInDB(pool *pgxpool.Pool, gid int, uid string) error {
func removeUserFromGroupInDB(pool *pgxpool.Pool, gid int, uid string) error {
_, err := pool.Exec(context.Background(), "DELETE from group_roles WHERE group_id = $1 AND user_id = $2", gid, uid)

if err != nil {
Expand All @@ -39,7 +43,7 @@ func RemoveUserFromGroupInDB(pool *pgxpool.Pool, gid int, uid string) error {
}

// GetGroupIDByUIDFromDB returns the groupID of a user given their UID
func GetGroupMemberByUIDFromDB(pool *pgxpool.Pool, uid string) (models.GroupRole, error) {
func getGroupMemberByUIDFromDB(pool *pgxpool.Pool, uid string) (models.GroupRole, error) {
var groupMember models.GroupRole
err := pool.QueryRow(context.Background(), "SELECT * FROM group_roles WHERE user_id = $1", uid).Scan(&groupMember.GroupID, &groupMember.UserID, &groupMember.Role)

Expand All @@ -52,7 +56,7 @@ func GetGroupMemberByUIDFromDB(pool *pgxpool.Pool, uid string) (models.GroupRole
}

// Get all group roles from the DB
func GetAllGroupRolesFromDB(pool *pgxpool.Pool, gid int) ([]models.GroupRole, error) {
func getAllGroupRolesFromDB(pool *pgxpool.Pool, gid int) ([]models.GroupRole, error) {
rows, err := pool.Query(context.Background(), "SELECT group_id, user_id, role FROM group_roles WHERE group_id = $1;", gid)

if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions backend/schema/groups/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ type PgModel struct {
func CareGroups(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {
careGroups := v1.Group("")
{
careGroups.POST("/create/:groupName", c.CreateCareGroups)
careGroups.POST("/create/:groupName", c.createCareGroups)

group := v1.Group("/:groupId")
{
group.GET("", c.GetGroupByGroupId)
group.POST("add", c.AddUserCareGroup)
group.GET("", c.getGroupByGroupId)
group.POST("add", c.addUserCareGroup)
}

}
Expand All @@ -39,9 +39,9 @@ func CareGroups(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {
//
// @success 200 {object} int
// @router /group/create/{groupName} [post]
func (pg *PgModel) CreateCareGroups(c *gin.Context) {
func (pg *PgModel) createCareGroups(c *gin.Context) {
groupName := c.Param("groupName")
careGroups, err := CreateCareGroupsFromDB(pg.Conn, groupName)
careGroups, err := createCareGroupsFromDB(pg.Conn, groupName)

if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
Expand All @@ -68,7 +68,7 @@ type GroupMember struct {
// @success 200 {object} int
// @failure 400 {object} string
// @router /group/{groupId}/add [post]
func (pg *PgModel) AddUserCareGroup(c *gin.Context) {
func (pg *PgModel) addUserCareGroup(c *gin.Context) {
var requestBody GroupMember
groupId := c.Param("groupId")

Expand All @@ -78,7 +78,7 @@ func (pg *PgModel) AddUserCareGroup(c *gin.Context) {
return
}

id, err := AddUserCareGroupFromDB(pg.Conn, groupId, requestBody)
id, err := addUserCareGroupFromDB(pg.Conn, groupId, requestBody)

if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
Expand All @@ -100,9 +100,9 @@ func (pg *PgModel) AddUserCareGroup(c *gin.Context) {
// @success 200 {object} models.CareGroup
// @failure 400 {object} string
// @router /group/{groupId} [get]
func (pg *PgModel) GetGroupByGroupId(c *gin.Context) {
func (pg *PgModel) getGroupByGroupId(c *gin.Context) {
groupId := c.Param("groupId")
group, err := GetGroupFromDB(pg.Conn, groupId)
group, err := getGroupFromDB(pg.Conn, groupId)

if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
Expand Down
6 changes: 3 additions & 3 deletions backend/schema/groups/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
)

func CreateCareGroupsFromDB(conn *pgxpool.Pool, groupName string) (int, error) {
func createCareGroupsFromDB(conn *pgxpool.Pool, groupName string) (int, error) {

var caregroup models.CareGroup

Expand All @@ -24,7 +24,7 @@ func CreateCareGroupsFromDB(conn *pgxpool.Pool, groupName string) (int, error) {

}

func AddUserCareGroupFromDB(conn *pgxpool.Pool, groupId string, groupMember GroupMember) (int, error) {
func addUserCareGroupFromDB(conn *pgxpool.Pool, groupId string, groupMember GroupMember) (int, error) {

var returningGroupId int
err := conn.QueryRow(context.Background(), "INSERT INTO group_roles (group_id, user_id, role) VALUES ($1, $2, $3) RETURNING group_id", groupId, groupMember.UserId, groupMember.Role).Scan(&returningGroupId)
Expand All @@ -39,7 +39,7 @@ func AddUserCareGroupFromDB(conn *pgxpool.Pool, groupId string, groupMember Grou
}

// Return all members of a group (by user_id)
func GetGroupFromDB(conn *pgxpool.Pool, groupId string) (models.CareGroup, error) {
func getGroupFromDB(conn *pgxpool.Pool, groupId string) (models.CareGroup, error) {
var caregroup models.CareGroup
groupIdInt, _ := strconv.Atoi(groupId)
err := conn.QueryRow(context.Background(), "SELECT * FROM care_group WHERE group_id = $1", groupIdInt).Scan(&caregroup.GroupID, &caregroup.GroupName, &caregroup.DateCreated)
Expand Down
24 changes: 12 additions & 12 deletions backend/schema/labels/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func LabelGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {

labels := v1.Group(":groupId/labels")
{
labels.POST("", c.CreateNewLabel)
labels.GET("", c.GetLabelsByGroup)
labels.DELETE(":lname", c.DeleteLabel)
labels.PATCH(":lname", c.EditLabel)
labels.POST("", c.createNewLabel)
labels.GET("", c.getLabelsByGroup)
labels.DELETE(":lname", c.deleteLabel)
labels.PATCH(":lname", c.editLabel)
}

return labels
Expand All @@ -37,10 +37,10 @@ func LabelGroup(v1 *gin.RouterGroup, c *PgModel) *gin.RouterGroup {
// @success 200 {array} models.Label
// @failure 400 {object} string
// @router /group/{groupId}/labels [GET]
func (pg *PgModel) GetLabelsByGroup(c *gin.Context) {
func (pg *PgModel) getLabelsByGroup(c *gin.Context) {
group_id := c.Param("groupId")

labels, err := GetLabelsByGroupFromDB(pg.Conn, group_id)
labels, err := getLabelsByGroupFromDB(pg.Conn, group_id)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
Expand All @@ -66,7 +66,7 @@ type LabelData struct {
// @success 200 {object} models.Label
// @failure 400 {object} string
// @router /group/{groupId}/labels [POST]
func (pg *PgModel) CreateNewLabel(c *gin.Context) {
func (pg *PgModel) createNewLabel(c *gin.Context) {
var requestBody LabelData
group_id := c.Param("groupId")

Expand All @@ -78,7 +78,7 @@ func (pg *PgModel) CreateNewLabel(c *gin.Context) {

id, _ := strconv.Atoi(group_id)

label, err := CreateNewLabelInDB(pg.Conn, id, requestBody)
label, err := createNewLabelInDB(pg.Conn, id, requestBody)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
Expand All @@ -99,11 +99,11 @@ func (pg *PgModel) CreateNewLabel(c *gin.Context) {
// @success 200 {object} string
// @failure 400 {object} string
// @router /group/{groupId}/labels/{lname} [DELETE]
func (pg *PgModel) DeleteLabel(c *gin.Context) {
func (pg *PgModel) deleteLabel(c *gin.Context) {
group_id := c.Param("groupId")
label_name := c.Param("lname")

err := DeleteLabelFromDB(pg.Conn, group_id, label_name)
err := deleteLabelFromDB(pg.Conn, group_id, label_name)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
Expand All @@ -125,7 +125,7 @@ func (pg *PgModel) DeleteLabel(c *gin.Context) {
// @success 200 {object} models.Label
// @failure 400 {object} string
// @router /group/{groupId}/labels/{lname} [PATCH]
func (pg *PgModel) EditLabel(c *gin.Context) {
func (pg *PgModel) editLabel(c *gin.Context) {
group_id := c.Param("groupId")
label_name := c.Param("lname")

Expand All @@ -136,7 +136,7 @@ func (pg *PgModel) EditLabel(c *gin.Context) {
return
}

label, err := EditLabelInDB(pg.Conn, group_id, label_name, requestBody)
label, err := editLabelInDB(pg.Conn, group_id, label_name, requestBody)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
Expand Down
Loading

0 comments on commit a6c6050

Please sign in to comment.