Skip to content

Commit

Permalink
WIP: first working release
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-iaco committed Sep 29, 2022
1 parent 1822cfd commit 1c40c8e
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 80 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
.github/
k8s/
104 changes: 104 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
name: Docker

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

on:
push:
branches: [ "master" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
paths-ignore:
- 'k8s/**'

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}


jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@7e0881f8fe90b25e305bbf0309761e9314607e25
with:
cosign-release: 'v1.9.0'


# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf
with:
install: true

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=raw,value=latest
type=sha
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
with:
context: .
platforms: linux/amd64, linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
env:
COSIGN_EXPERIMENTAL: "true"
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign {}@${{ steps.build-and-push.outputs.digest }}
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang:1.19.1-alpine as builder

WORKDIR /app

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

COPY . ./

RUN go build -o /food-track-be

EXPOSE 8080

FROM alpine

COPY --from=builder /food-track-be .

CMD [ "/food-details-integrator-be" ]
38 changes: 22 additions & 16 deletions controller/FoodConsumptionController.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,84 +18,90 @@ func NewFoodConsumptionController(foodConsumptionService *service.FoodConsumptio
func (s *FoodConsumptionController) FindAllConsumptionForMeal(c *gin.Context) {
mealId, err := uuid.Parse(c.Param("mealId"))
if err != nil {
c.JSON(500, dto.BaseResponse[any]{
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
foodConsumptionDtos, err := s.foodConsumptionService.FindAllFoodConsumptionForMeal(mealId)
if err != nil {
c.JSON(500, dto.BaseResponse[any]{
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
c.JSON(200, dto.BaseResponse[[]*dto.FoodConsumptionDto]{
Body: foodConsumptionDtos,
ErrorMessage: "",
Body: foodConsumptionDtos,
})
}

func (s *FoodConsumptionController) AddFoodConsumption(c *gin.Context) {
mealId, err := uuid.Parse(c.Param("mealId"))
if err != nil {
c.JSON(400, dto.BaseResponse[any]{
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
var foodConsumptionDto dto.FoodConsumptionDto
err = c.BindJSON(&foodConsumptionDto)
if err != nil {
c.JSON(400, dto.BaseResponse[any]{
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
foodConsumptionDto, err = s.foodConsumptionService.CreateFoodConsumptionForMeal(mealId, foodConsumptionDto)
if err != nil {
c.JSON(500, dto.BaseResponse[any]{
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
c.JSON(200, dto.BaseResponse[dto.FoodConsumptionDto]{
Body: foodConsumptionDto,
ErrorMessage: "",
Body: foodConsumptionDto,
})
}

func (s *FoodConsumptionController) UpdateFoodConsumption(c *gin.Context) {
mealId, err := uuid.Parse(c.Param("mealId"))
if err != nil {
c.JSON(400, dto.BaseResponse[any]{
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
var foodConsumptionDto dto.FoodConsumptionDto
c.BindJSON(&foodConsumptionDto)
foodConsumptionDto, err = s.foodConsumptionService.UpdateFoodConsumptionForMeal(mealId, foodConsumptionDto)
if err != nil {
c.JSON(500, dto.BaseResponse[any]{
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
c.JSON(200, dto.BaseResponse[dto.FoodConsumptionDto]{
Body: foodConsumptionDto,
ErrorMessage: "",
Body: foodConsumptionDto,
})
}

func (s *FoodConsumptionController) DeleteFoodConsumption(c *gin.Context) {
mealId, err := uuid.Parse(c.Param("mealId"))
foodConsumptionId, err := uuid.Parse(c.Param("foodConsumptionId"))
if err != nil {
c.JSON(500, dto.BaseResponse[any]{
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
err = s.foodConsumptionService.DeleteFoodConsumptionForMeal(mealId, foodConsumptionId)
if err != nil {
c.JSON(500, dto.BaseResponse[any]{
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
c.JSON(200, dto.BaseResponse[any]{
ErrorMessage: "",
Body: err != nil,
})
}
71 changes: 54 additions & 17 deletions controller/MealController.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,88 @@ func NewMealController(mealService *service.MealService) *MealController {

func (s *MealController) FindAllMeals(c *gin.Context) {
mealDtos, err := s.mealService.FindAll()
response := dto.BaseResponse[[]*dto.MealDto]{
Body: mealDtos,
ErrorMessage: err.Error(),
if err != nil {
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
response := dto.BaseResponse[[]dto.MealDto]{
Body: mealDtos,
}
c.JSON(200, response)
}

func (s *MealController) FindMealById(c *gin.Context) {
id, _ := uuid.Parse(c.Param("id"))
id, _ := uuid.Parse(c.Param("mealId"))
mealDto, err := s.mealService.FindById(id)
if err != nil {
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
response := dto.BaseResponse[dto.MealDto]{
Body: mealDto,
ErrorMessage: err.Error(),
Body: mealDto,
}
c.JSON(200, response)
}

func (s *MealController) CreateMeal(c *gin.Context) {
var mealDto dto.MealDto
c.BindJSON(&mealDto)
mealDto, err := s.mealService.Create(mealDto)
err := c.BindJSON(&mealDto)
if err != nil {
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
mealDto, err = s.mealService.Create(mealDto)
if err != nil {
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
response := dto.BaseResponse[dto.MealDto]{
Body: mealDto,
ErrorMessage: err.Error(),
Body: mealDto,
}
c.JSON(200, response)
}

func (s *MealController) UpdateMeal(c *gin.Context) {
var mealDto dto.MealDto
c.BindJSON(&mealDto)
mealDto, err := s.mealService.Update(mealDto)
err := c.BindJSON(&mealDto)
if err != nil {
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
mealDto, err = s.mealService.Update(mealDto)
if err != nil {
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
response := dto.BaseResponse[dto.MealDto]{
Body: mealDto,
ErrorMessage: err.Error(),
Body: mealDto,
}
c.JSON(200, response)
}

func (s *MealController) DeleteMeal(c *gin.Context) {
id, _ := uuid.Parse(c.Param("id"))
id, _ := uuid.Parse(c.Param("mealsId"))
err := s.mealService.Delete(id)
if err != nil {
c.AbortWithStatusJSON(200, dto.BaseResponse[any]{
ErrorMessage: err.Error(),
})
return
}
response := dto.BaseResponse[bool]{
Body: err == nil,
ErrorMessage: err.Error(),
Body: err == nil,
}
c.JSON(200, response)
}
15 changes: 9 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ module food-track-be

go 1.19

require (
github.com/gin-gonic/gin v1.8.1
github.com/google/uuid v1.3.0
github.com/mashingan/smapping v0.1.19
github.com/uptrace/bun v1.1.8
github.com/uptrace/bun/dialect/pgdialect v1.1.8
github.com/uptrace/bun/driver/pgdriver v1.1.8
)

require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.8.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mashingan/smapping v0.1.19 // indirect
github.com/mattn/go-isatty v0.0.16 // 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.0.5 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/uptrace/bun v1.1.8 // indirect
github.com/uptrace/bun/dialect/pgdialect v1.1.8 // indirect
github.com/uptrace/bun/driver/pgdriver v1.1.8 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect
Expand Down
Loading

0 comments on commit 1c40c8e

Please sign in to comment.