forked from taipei-doit/Taipei-City-Dashboard
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:rakechen-0307/Taipei-City-Dashboard…
… into getLocation
- Loading branch information
Showing
72 changed files
with
1,707 additions
and
954 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
Taipei-City-Dashboard-BE/app/controllers/contributor.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package controllers | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"TaipeiCityDashboardBE/app/models" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
/* | ||
GetAllContributors returns the contributor information | ||
GET /api/v1/contributor | ||
*/ | ||
func GetAllContributors(c *gin.Context) { | ||
type contributorQuery struct { | ||
PageSize int `form:"pagesize"` | ||
PageNum int `form:"pagenum"` | ||
Sort string `form:"sort"` | ||
Order string `form:"order"` | ||
} | ||
|
||
// Get query parameters | ||
var query contributorQuery | ||
c.ShouldBindQuery(&query) | ||
|
||
contributors, totalContributors, err := models.GetAllContributors(query.PageSize, query.PageNum, query.Sort, query.Order) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"status": "success", "total": totalContributors, "data": contributors}) | ||
} | ||
|
||
/* | ||
CreateContributor creates a new contributor | ||
POST /api/v1/contributor | ||
*/ | ||
func CreateContributor(c *gin.Context) { | ||
var contributor models.Contributor | ||
|
||
// Bind the contributor data | ||
if err := c.ShouldBindJSON(&contributor); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": err.Error()}) | ||
return | ||
} | ||
|
||
if contributor.UserID == "" || contributor.UserName == "" || contributor.Image == "" || contributor.Link == "" { | ||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": "user_id, user_name, image and link info is required"}) | ||
return | ||
} | ||
|
||
contributor, err := models.CreateContributor(contributor.UserID, contributor.UserName, contributor.Image, contributor.Link, contributor.Identity, contributor.Description, contributor.Include) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"status": "success", "data": contributor}) | ||
} | ||
|
||
/* | ||
UpdateContributor updates the contributor information | ||
PATCH /api/v1/contributor/:id | ||
*/ | ||
func UpdateContributor(c *gin.Context) { | ||
ID, err := strconv.Atoi(c.Param("id")) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": "Invalid contributor ID"}) | ||
return | ||
} | ||
|
||
// 1. Check if the contributor exists | ||
contributor, err := models.GetContributorByID(ID) | ||
if err != nil { | ||
c.JSON(http.StatusNotFound, gin.H{"error": "No contributor found"}) | ||
return | ||
} | ||
|
||
// 2. Bind the JSON body to the contributor struct | ||
err = c.ShouldBindJSON(&contributor) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
// 3. Update the contributor | ||
contributor, err = models.UpdateContributor(ID, contributor.UserID, contributor.UserName, contributor.Image, contributor.Link, contributor.Identity, contributor.Description, contributor.Include) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update contributor"}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"status": "success", "data": contributor}) | ||
} | ||
|
||
/* | ||
DeleteContributor deletes a contributor from the database. | ||
DELETE /api/v1/contributor/:id | ||
*/ | ||
func DeleteContributor(c *gin.Context) { | ||
// 1. Get the contributor ID from the context | ||
ID, err := strconv.Atoi(c.Param("id")) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": "Invalid contributor ID"}) | ||
return | ||
} | ||
|
||
// 2. Delete the contributor | ||
contributorStatus, err := models.DeleteContributorByID(ID) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"status": "success", "contributor_deleted": contributorStatus}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
/* ----- Models ----- */ | ||
|
||
type Contributor struct { | ||
ID int64 `json:"id" gorm:"column:id;autoincrement;primaryKey"` | ||
UserID string `json:"user_id" gorm:"column:user_id;type:varchar;not null"` | ||
UserName string `json:"user_name" gorm:"column:user_name;type:varchar;not null"` | ||
Image string `json:"image" gorm:"column:image;type:text"` | ||
Link string `json:"link" gorm:"column:link;type:text;not null"` | ||
Identity *string `json:"identity" gorm:"column:identity;type:varchar"` | ||
Description *string `json:"description" gorm:"column:description;type:text"` | ||
Include *bool `json:"include" gorm:"column:include;type:boolean;default:false;not null"` | ||
CreatedAt time.Time `json:"created_at" gorm:"column:created_at;type:timestamp with time zone;not null"` | ||
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at;type:timestamp with time zone;not null"` | ||
} | ||
|
||
/* ----- Handlers ----- */ | ||
|
||
func GetAllContributors(pageSize int, pageNum int, sort string, order string) (contributors []Contributor, totalContributors int64, err error) { | ||
tempDB := DBManager.Table("contributors") | ||
|
||
// Count the total amount of contributors | ||
tempDB.Count(&totalContributors) | ||
|
||
// Sort the contributors | ||
if sort != "" { | ||
tempDB = tempDB.Order("contributors." + sort + " " + order) | ||
} else { | ||
tempDB = tempDB.Order("contributors.id asc") | ||
} | ||
|
||
// Paginate the contributors | ||
if pageSize > 0 { | ||
tempDB = tempDB.Limit(pageSize) | ||
if pageNum > 0 { | ||
tempDB = tempDB.Offset((pageNum - 1) * pageSize) | ||
} | ||
} | ||
|
||
// Get the contributors | ||
err = tempDB.Find(&contributors).Error | ||
|
||
return contributors, totalContributors, err | ||
} | ||
|
||
func GetContributorByID(ID int) (contributor Contributor, err error) { | ||
err = DBManager.Table("contributors").Where("id = ?", ID).First(&contributor).Error | ||
return contributor, err | ||
} | ||
|
||
func CreateContributor(userID string, userName string, image string, link string, identity *string, description *string, include *bool) (contributor Contributor, err error) { | ||
contributor.UserID = userID | ||
contributor.UserName = userName | ||
contributor.Image = image | ||
contributor.Link = link | ||
contributor.CreatedAt = time.Now() | ||
contributor.UpdatedAt = time.Now() | ||
contributor.Identity = identity | ||
contributor.Description = description | ||
contributor.Include = include | ||
err = DBManager.Create(&contributor).Error | ||
return contributor, err | ||
} | ||
|
||
func UpdateContributor(ID int, userID string, userName string, image string, link string, identity *string, description *string, include *bool) (contributor Contributor, err error) { | ||
err = DBManager.Model(&Contributor{}).Where("id = ?", ID).Updates(map[string]interface{}{ | ||
"user_id": userID, "user_name": userName, "image": image, "link": link, "updated_at": time.Now(), | ||
"identity": identity, "description": description, "include": include, | ||
}).Error | ||
if err != nil { | ||
return contributor, err | ||
} | ||
|
||
err = DBManager.Table("contributors").Where("id = ?", ID).First(&contributor).Error | ||
if err != nil { | ||
return contributor, err | ||
} | ||
|
||
return contributor, err | ||
} | ||
|
||
func DeleteContributorByID(ID int) (contributorStatus bool, err error) { | ||
err = DBManager.Model(&Contributor{}).Where("id = ?", ID).Delete(&Contributor{}).Error | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return true, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.