Adding/Changing post/put/delete endpoints

parent 62e834ff
......@@ -415,137 +415,95 @@ func APIv1GetProduct(c *gin.Context) {
c.JSON(http.StatusOK, Product)
}
// APIv1GetVersions is the GET endpoint for versions in API v1
func APIv1GetVersions(c *gin.Context) {
var Versions []database.Version
query := database.Db.Model(&database.Version{})
query = filters(query, c)
query = order(query, c)
query, total, limit, offset := paginate(query, c)
query.Find(&Versions)
c.JSON(http.StatusOK, gin.H{"Items": Versions, "ItemCount": total, "Limit": limit, "Offset": offset})
}
// APIv1GetVersion is the GET endpoint for version in API v1
func APIv1GetVersion(c *gin.Context) {
var Version database.Version
database.Db.First(&Version, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, Version)
}
// APIv1GetUsers is the GET endpoint for users in API v1
func APIv1GetUsers(c *gin.Context) {
var Users []database.User
query := database.Db.Model(&database.User{})
query = filters(query, c)
query = order(query, c)
query, total, limit, offset := paginate(query, c)
query.Find(&Users)
c.JSON(http.StatusOK, gin.H{"Items": Users, "ItemCount": total, "Limit": limit, "Offset": offset})
}
// APIv1GetUser is the GET endpoint for user in API v1
func APIv1GetUser(c *gin.Context) {
var User database.User
database.Db.First(&User, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, User)
}
// APIv1GetComments is the GET endpoint for comments in API v1
func APIv1GetComments(c *gin.Context) {
var Comments []database.Comment
query := database.Db.Model(&database.Comment{})
query = filters(query, c)
query = order(query, c)
query, total, limit, offset := paginate(query, c)
query.Find(&Comments)
c.JSON(http.StatusOK, gin.H{"Items": Comments, "ItemCount": total, "Limit": limit, "Offset": offset})
}
// APIv1GetComment is the GET endpoint for comment in API v1
func APIv1GetComment(c *gin.Context) {
var Comment database.Comment
database.Db.First(&Comment, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, Comment)
}
/// !!!-1-1-1 API OLD 1-1-1-!!! ///
// ---------------------------- Product endpoints ------------------------------
// APINewProduct processes the new product endpoint
func APINewProduct(c *gin.Context) {
// APIv1NewProduct processes the new product endpoint
func APIv1NewProduct(c *gin.Context) {
var Product database.Product
if err := c.BindJSON(&Product); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
Product.ID = uuid.NewV4()
if err := database.Db.Create(&Product).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusCreated, gin.H{"error": nil, "object": Product})
c.JSON(http.StatusCreated, gin.H{"Error": nil, "Item": Product})
}
}
// APIUpdateProduct processes the update product endpoint
func APIUpdateProduct(c *gin.Context) {
// APIv1UpdateProduct processes the update product endpoint
func APIv1UpdateProduct(c *gin.Context) {
var Product database.Product
var Product2 database.Product
if err := c.BindJSON(&Product2); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
if err := database.Db.Where("id = ?", uuid.FromStringOrNil(c.Param("id"))).Find(&Product).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
Product2.CreatedAt = Product.CreatedAt
Product2.ID = Product.ID
copier.Copy(&Product, &Product2)
if err := database.Db.Save(&Product).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusAccepted, gin.H{"error": nil, "object": Product})
c.JSON(http.StatusAccepted, gin.H{"Error": nil, "Item": Product})
}
}
// APIDeleteProduct processes the delete product endpoint
func APIDeleteProduct(c *gin.Context) {
// APIv1DeleteProduct processes the delete product endpoint
func APIv1DeleteProduct(c *gin.Context) {
if err := database.Db.Delete(database.Product{}, "id = ?", uuid.FromStringOrNil(c.Param("id"))).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusOK, gin.H{"error": nil, "object": nil})
c.JSON(http.StatusOK, gin.H{"Error": nil, "Item": nil})
}
}
// ---------------------------- Version endpoints ------------------------------
// APIv1GetVersions is the GET endpoint for versions in API v1
func APIv1GetVersions(c *gin.Context) {
var Versions []database.Version
query := database.Db.Model(&database.Version{})
query = filters(query, c)
query = order(query, c)
query, total, limit, offset := paginate(query, c)
query.Find(&Versions)
c.JSON(http.StatusOK, gin.H{"Items": Versions, "ItemCount": total, "Limit": limit, "Offset": offset})
}
// APIv1GetVersion is the GET endpoint for version in API v1
func APIv1GetVersion(c *gin.Context) {
var Version database.Version
database.Db.First(&Version, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, Version)
}
// APINewVersion processes the new product form
func APINewVersion(c *gin.Context) {
// APIv1NewVersion processes the new product form
func APIv1NewVersion(c *gin.Context) {
var Version database.Version
if err := c.BindJSON(&Version); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
Version.ID = uuid.NewV4()
if err := database.Db.Create(&Version).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusCreated, gin.H{"error": nil, "object": Version})
c.JSON(http.StatusCreated, gin.H{"Error": nil, "Item": Version})
}
}
// APIUpdateVersion processes the new product form
func APIUpdateVersion(c *gin.Context) {
// APIv1UpdateVersion processes the new product form
func APIv1UpdateVersion(c *gin.Context) {
var Version database.Version
var Version2 database.Version
if err := c.BindJSON(&Version2); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
if err := database.Db.Where("id = ?", uuid.FromStringOrNil(c.Param("id"))).Find(&Version).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
Version2.CreatedAt = Version.CreatedAt
......@@ -555,65 +513,145 @@ func APIUpdateVersion(c *gin.Context) {
}
copier.Copy(&Version, &Version2)
if err := database.Db.Save(&Version).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusAccepted, gin.H{"error": nil, "object": Version})
c.JSON(http.StatusAccepted, gin.H{"Error": nil, "Item": Version})
}
}
// APIDeleteVersion processes the delete version endpoint
func APIDeleteVersion(c *gin.Context) {
// APIv1DeleteVersion processes the delete version endpoint
func APIv1DeleteVersion(c *gin.Context) {
if err := database.Db.Delete(database.Version{}, "id = ?", uuid.FromStringOrNil(c.Param("id"))).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusOK, gin.H{"error": nil, "object": nil})
c.JSON(http.StatusOK, gin.H{"Error": nil, "Item": nil})
}
}
// ------------------------------ User endpoints -------------------------------
// APIv1GetUsers is the GET endpoint for users in API v1
func APIv1GetUsers(c *gin.Context) {
var Users []database.User
query := database.Db.Model(&database.User{})
query = filters(query, c)
query = order(query, c)
query, total, limit, offset := paginate(query, c)
query.Find(&Users)
c.JSON(http.StatusOK, gin.H{"Items": Users, "ItemCount": total, "Limit": limit, "Offset": offset})
}
// APIv1GetUser is the GET endpoint for user in API v1
func APIv1GetUser(c *gin.Context) {
var User database.User
database.Db.First(&User, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, User)
}
// APINewUser processes the new user endpoint
func APINewUser(c *gin.Context) {
// APIv1NewUser processes the new user endpoint
func APIv1NewUser(c *gin.Context) {
var User database.User
if err := c.BindJSON(&User); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
User.ID = uuid.NewV4()
if err := database.Db.Create(&User).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusCreated, gin.H{"error": nil, "object": User})
c.JSON(http.StatusCreated, gin.H{"Error": nil, "Item": User})
}
}
// APIUpdateUser processes the update user endpoint
func APIUpdateUser(c *gin.Context) {
// APIv1UpdateUser processes the update user endpoint
func APIv1UpdateUser(c *gin.Context) {
var User database.User
var User2 database.User
if err := c.BindJSON(&User2); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
if err := database.Db.Where("id = ?", uuid.FromStringOrNil(c.Param("id"))).Find(&User).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
User2.CreatedAt = User.CreatedAt
User2.ID = User.ID
copier.Copy(&User, &User2)
if err := database.Db.Save(&User).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusAccepted, gin.H{"error": nil, "object": User})
c.JSON(http.StatusAccepted, gin.H{"Error": nil, "Item": User})
}
}
// APIDeleteUser processes the delete user endpoint
func APIDeleteUser(c *gin.Context) {
// APIv1DeleteUser processes the delete user endpoint
func APIv1DeleteUser(c *gin.Context) {
if err := database.Db.Delete(database.User{}, "id = ?", uuid.FromStringOrNil(c.Param("id"))).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "object": nil})
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusOK, gin.H{"Error": nil, "Item": nil})
}
}
// APIv1GetComments is the GET endpoint for comments in API v1
func APIv1GetComments(c *gin.Context) {
var Comments []database.Comment
query := database.Db.Model(&database.Comment{})
query = filters(query, c)
query = order(query, c)
query, total, limit, offset := paginate(query, c)
query.Find(&Comments)
c.JSON(http.StatusOK, gin.H{"Items": Comments, "ItemCount": total, "Limit": limit, "Offset": offset})
}
// APIv1GetComment is the GET endpoint for comment in API v1
func APIv1GetComment(c *gin.Context) {
var Comment database.Comment
database.Db.First(&Comment, "id = ?", c.Param("id"))
c.JSON(http.StatusOK, Comment)
}
// APIv1NewComment processes the new comment endpoint
func APIv1NewComment(c *gin.Context) {
var Comment database.Comment
if err := c.BindJSON(&Comment); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
Comment.ID = uuid.NewV4()
if err := database.Db.Create(&Comment).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusCreated, gin.H{"Error": nil, "Item": Comment})
}
}
// APIv1UpdateComment processes the update comment endpoint
func APIv1UpdateComment(c *gin.Context) {
var Comment database.Comment
var Comment2 database.Comment
if err := c.BindJSON(&Comment2); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
if err := database.Db.Where("id = ?", uuid.FromStringOrNil(c.Param("id"))).Find(&Comment).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
return
}
Comment2.CreatedAt = Comment.CreatedAt
Comment2.ID = Comment.ID
copier.Copy(&Comment, &Comment2)
if err := database.Db.Save(&Comment).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusAccepted, gin.H{"Error": nil, "Item": Comment})
}
}
// APIv1DeleteComment processes the delete comment endpoint
func APIv1DeleteComment(c *gin.Context) {
if err := database.Db.Delete(database.Comment{}, "id = ?", uuid.FromStringOrNil(c.Param("id"))).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Error": err.Error(), "Item": nil})
} else {
c.JSON(http.StatusOK, gin.H{"error": nil, "object": nil})
c.JSON(http.StatusOK, gin.H{"Error": nil, "Item": nil})
}
}
......@@ -68,29 +68,27 @@ func initRouter() *gin.Engine {
apiv1.GET("/products", APIv1GetProducts)
apiv1.GET("/products/:id", APIv1GetProduct)
apiv1.POST("/products", APIv1NewProduct)
apiv1.PUT("/products/:id", APIv1UpdateProduct)
apiv1.DELETE("/products/:id", APIv1DeleteProduct)
apiv1.GET("/versions", APIv1GetVersions)
apiv1.GET("/versions/:id", APIv1GetVersion)
apiv1.POST("/versions", APIv1NewVersion)
apiv1.PUT("/versions/:id", APIv1UpdateVersion)
apiv1.DELETE("/versions/:id", APIv1DeleteVersion)
apiv1.GET("/users", APIv1GetUsers)
apiv1.GET("/users/:id", APIv1GetUser)
apiv1.POST("/users", APIv1NewUser)
apiv1.PUT("/users/:id", APIv1UpdateUser)
apiv1.DELETE("/users/:id", APIv1DeleteUser)
apiv1.GET("/comments", APIv1GetComments)
apiv1.GET("/comments/:id", APIv1GetComment)
//////// OLD
apiv1.POST("/products", APINewProduct)
apiv1.PUT("/products/:id", APIUpdateProduct)
apiv1.DELETE("/products/:id", APIDeleteProduct)
apiv1.POST("/versions", APINewVersion)
apiv1.PUT("/versions/:id", APIUpdateVersion)
apiv1.DELETE("/versions/:id", APIDeleteVersion)
apiv1.POST("/users", APINewUser)
apiv1.PUT("/users/:id", APIUpdateUser)
apiv1.DELETE("/users/:id", APIDeleteUser)
apiv1.POST("/comments", APIv1NewComment)
apiv1.PUT("/comments/:id", APIv1UpdateComment)
apiv1.DELETE("/comments/:id", APIv1DeleteComment)
// simple-breakpad endpoints
router.GET("/", GetIndex)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment