Skip to content

Commit

Permalink
Create stable api v3 (#7)
Browse files Browse the repository at this point in the history
* Update dependencies

* Update go version

* feat: implement missing crud handlers for investors locations and statuses

* unify api behavior

* add possibility to include decision date in location creation and update

* update readme

* preload locations status when fetching office

* update application schema

* fix investment status preloading when fetching investor

* add package publishing gh action

* add missing argument

* create minimal docker image configuration

* remove duplicated functions

* update dependencies
  • Loading branch information
szarbartosz authored Nov 5, 2024
1 parent 5272a66 commit 002dd8c
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 94 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/publish-ghcr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: hedgeapp backend

on:
push:
branches: [master, develop]

jobs:
build-and-publish:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GH_PAT }}

- name: Set image tag
run: |
if [[ $GITHUB_REF == refs/heads/master ]]; then
IMAGE_TAG=latest
elif [[ $GITHUB_REF == refs/heads/develop ]]; then
IMAGE_TAG=develop
fi
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
- name: Build and push docker image
run: |
docker build --tag ghcr.io/szarbartosz/hedgeapp-back:$IMAGE_TAG .
docker push ghcr.io/szarbartosz/hedgeapp-back:$IMAGE_TAG
21 changes: 15 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
FROM golang:1.22.3-alpine
# build a binary
FROM golang:1.22.3-alpine AS builder

WORKDIR /app
RUN apk update && apk add --no-cache 'git=~2'

COPY go.mod go.sum ./
RUN go mod download

ENV GO111MODULE=on
WORKDIR /app
COPY . .

RUN go build -o main .
RUN go get -d -v

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /binary .

# build a small image
FROM alpine:3

WORKDIR /
COPY --from=builder /binary /main

EXPOSE 8080

CMD ["./main"]
ENTRYPOINT ["/main"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## dev quickstart :construction:

```bash
$ docker-compose up --build -d
$ docker compose up --build -d
```

---
Expand Down
2 changes: 1 addition & 1 deletion controllers/investorsController.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ 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"))
result := initializers.DB.Where("user_id = ?", user.(models.User).ID).Preload("Address").Preload("Locations").Preload("Locations.Status").First(&investor, c.Param("id"))

if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{
Expand Down
30 changes: 21 additions & 9 deletions controllers/locationsController.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ func CreateLocation(c *gin.Context) {
IssueDate: body.IssueDate,
InspectionDate: body.InspectionDate,
InspectionDone: false,
DecisionDate: body.DecisionDate,
DeforestationDate: body.DeforestationDate,
DeforestationDone: false,
PlantingDate: body.PlantingDate,
PlantingDone: false,
Application: models.Application{
UserID: user.(models.User).ID,
Signature: body.Application.Signature,
IsDeforestationCommercial: body.Application.IsDeforestationCommercial,
DeforestationCause: body.Application.DeforestationCause,
DeforestationDate: body.Application.DeforestationDate,
PlantingDate: body.Application.PlantingDate,
PlantingPlace: body.Application.PlantingPlace,
Species: body.Application.Species,
UserID: user.(models.User).ID,
Signature: body.Application.Signature,
IsCommercial: body.Application.IsCommercial,
DeforestationCause: body.Application.DeforestationCause,
DeforestationDate: body.Application.DeforestationDate,
PlantingDate: body.Application.PlantingDate,
PlantingPlace: body.Application.PlantingPlace,
Species: body.Application.Species,
},
Address: models.Address{
UserID: user.(models.User).ID,
Expand Down Expand Up @@ -86,6 +87,7 @@ func UpdateLocation(c *gin.Context) {
location.IssueDate = body.IssueDate
location.InspectionDate = body.InspectionDate
location.InspectionDone = body.InspectionDone
location.DecisionDate = body.DecisionDate
location.DeforestationDate = body.DeforestationDate
location.DeforestationDone = body.DeforestationDone
location.PlantingDate = body.PlantingDate
Expand All @@ -95,7 +97,7 @@ func UpdateLocation(c *gin.Context) {
location.InvestorID = body.InvestorID

location.Application.Signature = body.Application.Signature
location.Application.IsDeforestationCommercial = body.Application.IsDeforestationCommercial
location.Application.IsCommercial = body.Application.IsCommercial
location.Application.DeforestationCause = body.Application.DeforestationCause
location.Application.DeforestationDate = body.Application.DeforestationDate
location.Application.PlantingDate = body.Application.PlantingDate
Expand Down Expand Up @@ -127,6 +129,16 @@ func UpdateLocation(c *gin.Context) {

tx.Commit()

if err := tx.Save(&location.Address).Error; err != nil {
tx.Rollback()
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to update address",
})
return
}

tx.Commit()

c.JSON(http.StatusOK, location)
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/officeController.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func GetOffices(c *gin.Context) {
func GetSingleOffice(c *gin.Context) {
var office models.Office

result := initializers.DB.Preload("Address").Preload("Locations").First(&office, c.Param("id"))
result := initializers.DB.Preload("Address").Preload("Locations").Preload("Locations.Status").First(&office, c.Param("id"))

if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{
Expand Down
30 changes: 22 additions & 8 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
services:
database:
image: postgres:alpine
container_name: hedheapp-db
restart: always
container_name: db
restart: unless-stopped
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
ports:
- 9920:5432
- 5432:5432
networks:
- app-network
backend:
build: .
container_name: hedgeapp-back
restart: always
env_file:
- .env
backend:
build: .
container_name: backend
restart: unless-stopped
environment:
PORT: ${PORT}
GIN_MODE: ${GIN_MODE}
JWT_SECRET: ${JWT_SECRET}
ALLOWED_ORIGIN: ${ALLOWED_ORIGIN}
COOKIE_DOMAIN: ${COOKIE_DOMAIN}
SECURE_COOKIE: ${SECURE_COOKIE}
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
DB_NAME: ${DB_NAME}
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
ports:
- 4000:3000
- 8080:8080
depends_on:
- database
networks:
- app-network
env_file:
- .env

networks:
app-network:
Expand Down
40 changes: 21 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,46 @@ go 1.22
require (
github.com/gin-gonic/gin v1.10.0
github.com/go-playground/assert/v2 v2.2.0
github.com/golang-jwt/jwt/v4 v4.4.3
github.com/joho/godotenv v1.4.0
golang.org/x/crypto v0.23.0
gorm.io/driver/postgres v1.5.7
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde
github.com/golang-jwt/jwt/v4 v4.5.1
github.com/joho/godotenv v1.5.1
golang.org/x/crypto v0.28.0
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.12
)

require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/bytedance/sonic v1.12.4 // indirect
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
golang.org/x/arch v0.11.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 002dd8c

Please sign in to comment.