Skip to content

Commit

Permalink
Create stable api v2 (#6)
Browse files Browse the repository at this point in the history
* 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
szarbartosz authored Sep 7, 2024
1 parent dcf5ff5 commit 5272a66
Show file tree
Hide file tree
Showing 27 changed files with 788 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
build:
working_directory: ~/repo
docker:
- image: cimg/go:1.19
- image: cimg/go:1.22
steps:
- checkout
- restore_cache:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
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

Expand Down
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ POST /statuses
GET /statuses
```

### Developer
### Investor

#### create developer
#### create investor

```http
POST /developers
POST /investors
```

```js
Expand All @@ -78,10 +78,10 @@ POST /developers
}
```

#### get developers
#### get investors

```http
GET /developers
GET /investors
```

### Location
Expand All @@ -94,15 +94,15 @@ POST /locations

```js
{
status_id: int,
developer_id: int,
statusId: int,
investorId: int,
name: string,
issue_date: string, // ISO 8601
inspection_date: string, // ISO 8601
deforestation_date: string, // ISO 8601
planting_date: string, // ISO 8601
deforestation_done: boolean,
planting_done: boolean
issueDate: string, // ISO 8601
inspectionDate: string, // ISO 8601
deforestationDate: string, // ISO 8601
plantingDate: string, // ISO 8601
deforestationDone: boolean,
plantingDone: boolean
}
```

Expand All @@ -114,15 +114,15 @@ PUT /locations/:id

```js
{
status_id: int,
developer_id: int,
statusId: int,
investorId: int,
name: string,
issue_date: string, // ISO 8601
inspection_date: string, // ISO 8601
deforestation_date: string, // ISO 8601
planting_date: string, // ISO 8601
deforestation_done: boolean,
planting_done: boolean
issueDate: string, // ISO 8601
inspectionDate: string, // ISO 8601
deforestationDate: string, // ISO 8601
plantingDate: string, // ISO 8601
deforestationDone: boolean,
plantingDone: boolean
}
```

Expand Down
54 changes: 0 additions & 54 deletions controllers/developersController.go

This file was deleted.

161 changes: 161 additions & 0 deletions controllers/investorsController.go
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",
})
}
Loading

0 comments on commit 5272a66

Please sign in to comment.