-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove obsolete version signature * Update dependencies * Update go version * Update circle ci config * feat: update models * chore: adjust controllers * feat: implement header based auth * feat: adjust models * feat: implement missing crud handlers for investors locations and statuses * chore: introduce location application handling * add note related crud handlers * preload locations when fetching investor * introduce office model * unify api behavior * preload investor while fetching locations * add contact info to investor schema * fix preloading issues * preload office address and locations * add missing fields to data model * improve token extraction
- Loading branch information
1 parent
dcf5ff5
commit 5272a66
Showing
27 changed files
with
788 additions
and
207 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM golang:1.19.3-alpine | ||
FROM golang:1.22.3-alpine | ||
|
||
WORKDIR /app | ||
|
||
|
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package controllers | ||
|
||
import ( | ||
"main/initializers" | ||
"main/models" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func CreateInvestor(c *gin.Context) { | ||
user, _ := c.Get("user") | ||
var body models.Investor | ||
|
||
if c.BindJSON(&body) != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{ | ||
"error": "Invalid request body", | ||
}) | ||
return | ||
} | ||
|
||
investor := models.Investor{ | ||
Name: body.Name, | ||
UserID: user.(models.User).ID, | ||
ContactPerson: body.ContactPerson, | ||
Email: body.Email, | ||
Phone: body.Phone, | ||
Nip: body.Nip, | ||
Regon: body.Regon, | ||
Address: models.Address{ | ||
UserID: user.(models.User).ID, | ||
City: body.Address.City, | ||
Street: body.Address.Street, | ||
Number: body.Address.Number, | ||
ZipCode: body.Address.ZipCode, | ||
}} | ||
|
||
result := initializers.DB.Create(&investor) | ||
if result.Error != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": "Failed to create investor", | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, investor) | ||
} | ||
|
||
func UpdateInvestor(c *gin.Context) { | ||
user, _ := c.Get("user") | ||
var body models.Investor | ||
|
||
if c.BindJSON(&body) != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{ | ||
"error": "Invalid request body", | ||
}) | ||
return | ||
} | ||
|
||
var investor models.Investor | ||
result := initializers.DB.Preload("Address").Where("user_id = ?", user.(models.User).ID).Where("id = ?", c.Param("id")).First(&investor) | ||
if result.Error != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": "Investor not found", | ||
}) | ||
return | ||
} | ||
|
||
investor.Name = body.Name | ||
investor.ContactPerson = body.ContactPerson | ||
investor.Email = body.Email | ||
investor.Phone = body.Phone | ||
investor.Nip = body.Nip | ||
investor.Regon = body.Regon | ||
|
||
investor.Address.City = body.Address.City | ||
investor.Address.Street = body.Address.Street | ||
investor.Address.Number = body.Address.Number | ||
investor.Address.ZipCode = body.Address.ZipCode | ||
|
||
tx := initializers.DB.Begin() | ||
|
||
if err := tx.Save(&investor).Error; err != nil { | ||
tx.Rollback() | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": "Failed to update investor", | ||
}) | ||
return | ||
} | ||
|
||
if err := tx.Save(&investor.Address).Error; err != nil { | ||
tx.Rollback() | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": "Failed to update address", | ||
}) | ||
return | ||
} | ||
|
||
tx.Commit() | ||
|
||
c.JSON(http.StatusOK, investor) | ||
} | ||
|
||
func GetInvestors(c *gin.Context) { | ||
user, _ := c.Get("user") | ||
var investors []models.Investor | ||
|
||
result := initializers.DB.Where("user_id = ?", user.(models.User).ID).Preload("Address").Preload("Locations").Find(&investors) | ||
|
||
if result.Error != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": "Failed to get investors", | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, investors) | ||
} | ||
|
||
func GetSingleInvestor(c *gin.Context) { | ||
user, _ := c.Get("user") | ||
var investor models.Investor | ||
|
||
result := initializers.DB.Where("user_id = ?", user.(models.User).ID).Preload("Address").Preload("Locations").First(&investor, c.Param("id")) | ||
|
||
if result.Error != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": "Failed to get investor", | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, investor) | ||
} | ||
|
||
func DeleteInvestor(c *gin.Context) { | ||
user, _ := c.Get("user") | ||
var investor models.Investor | ||
|
||
result := initializers.DB.Where("user_id = ?", user.(models.User).ID).First(&investor, c.Param("id")) | ||
|
||
if result.Error != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": "Investor not found", | ||
}) | ||
return | ||
} | ||
|
||
deleteResult := initializers.DB.Delete(&investor) | ||
|
||
if deleteResult.Error != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": "Failed to delete investor: " + deleteResult.Error.Error(), | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "Investor deleted", | ||
}) | ||
} |
Oops, something went wrong.