Skip to content

Commit

Permalink
chore: implemented delete company route
Browse files Browse the repository at this point in the history
  • Loading branch information
baimamboukar committed Dec 26, 2023
1 parent 26f82be commit 665762a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
Binary file modified gin-bin
Binary file not shown.
16 changes: 12 additions & 4 deletions src/controllers/companies_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ func CreateCompany(context *gin.Context) {
}

func UpdateCompany(c *gin.Context) {
// Implement logic to update a company
c.JSON(200, gin.H{"message": "Update a company"})
}

func DeleteCompany(c *gin.Context) {
// Implement logic to delete a company by ID
// Implement logic to delete a company
companyID := c.Param("id")
c.JSON(200, gin.H{"message": "Delete company by ID", "id": companyID})
if companyID == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "Company ID is required"})
return
}
err := models.DeleteCompany(companyID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Company deleted successfully"})

}
43 changes: 42 additions & 1 deletion src/models/company.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import "gorm.io/gorm"
import (
"gorm.io/gorm"
)

type Company struct {
gorm.Model
Expand Down Expand Up @@ -31,10 +33,49 @@ type SocialMedia struct {
Instagram string `json:"instagram"`
}

// Saves a company to the database
func (company *Company) Save() (*Company, error) {
err := Database.Create(&company).Error
if err != nil {
return &Company{}, err
}
return company, nil
}

// Fetches all companies from the database
func FetchAllCompanies() (*[]Company, error) {
var companies []Company
err := Database.Find(&companies).Error
if err != nil {
return &[]Company{}, err
}
return &companies, nil
}

// Fetches a company from the database
func FetchCompany(id string) (*Company, error) {
var company Company
err := Database.Where("id = ?", id).First(&company).Error
if err != nil {
return &Company{}, err
}
return &company, nil
}

// Updates a company in the database
func UpdateCompany(id string, company *Company) (*Company, error) {
err := Database.Model(&Company{}).Where("id = ?", id).Updates(company).Error
if err != nil {
return &Company{}, err
}
return company, nil
}

// Deletes a company from the database
func DeleteCompany(id string) error {
err := Database.Where("id = ?", id).Delete(&Company{}).Error
if err != nil {
return err
}
return nil
}
5 changes: 2 additions & 3 deletions src/routes/companies.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
"github.com/baimamboukar/go-gin-docker-k8s/src/controllers"
)

// controller := &CompaniesController{}
func companiesGroupRouter(baseRouter *gin.RouterGroup) {

companies := baseRouter.Group("/companies")
companies.GET("/all", controllers.GetAllCompanies)
companies.GET("/:id", controllers.GetCompanyByID)
companies.GET("/get/:id", controllers.GetCompanyByID)
companies.POST("/create", controllers.CreateCompany)
companies.PATCH("/update", controllers.UpdateCompany)
companies.PUT("/update", controllers.UpdateCompany)
companies.DELETE("/:id", controllers.DeleteCompany)
companies.DELETE("/delete/:id", controllers.DeleteCompany)
}

2 comments on commit 665762a

@vercel
Copy link

@vercel vercel bot commented on 665762a Dec 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 665762a Dec 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.