Skip to content

Commit

Permalink
Update Update controller to update text field too
Browse files Browse the repository at this point in the history
  • Loading branch information
harshsinghvi committed Nov 17, 2023
1 parent 582d42b commit 6b27af3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
```bash
kubectl rollout restart deployment/name # to update image
kubectl get ingress # ingress exposed url
kubectl port-forward statefulset.apps/postgres 5432:5432
kubectl exec --stdin --tty pod/postgres-0 -- /bin/bash
aws eks update-kubeconfig --region ap-south-1 --name cluster
```
Expand All @@ -64,9 +65,7 @@ aws eks update-kubeconfig --region ap-south-1 --name cluster

```bash
docker buildx build --platform=linux/amd64 -t golang-postgres-kubernetes .

docker tag golang-postgres-kubernetes ghcr.io/harshsinghvi/golang-postgres-kubernetes:latest

docker push ghcr.io/harshsinghvi/golang-postgres-kubernetes:latest
```

Expand Down
21 changes: 19 additions & 2 deletions controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,15 @@ func EditTodo(c *gin.Context) {
todoId := c.Param("id")
var todo models.Todo
c.BindJSON(&todo)
completed := todo.Completed
_, err := database.Connection.Model(&models.Todo{}).Set("completed = ?", completed).Where("id = ?", todoId).Update()

querry := database.Connection.Model(&models.Todo{}).Set("completed = ?", todo.Completed)

if todo.Text != "" {
querry.Set("text = ?", todo.Text)
}

res, err := querry.Where("id = ?", todoId).Update()

if err != nil {
log.Printf("Error, Reason: %v\n", err)
c.JSON(http.StatusInternalServerError, gin.H{
Expand All @@ -102,6 +109,16 @@ func EditTodo(c *gin.Context) {
})
return
}

if res.RowsAffected() == 0 {
log.Printf("Error while update todo, Reason: \n")
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"message": "Todo not found",
})
return
}

c.JSON(http.StatusOK, gin.H{
"status": 200,
"message": "Todo Edited Successfully",
Expand Down
21 changes: 12 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ services:
networks:
- fullstack
environment:
- PORT=${PORT}
- POSTGRES_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=disable
- DB_HOST=${DB_HOST}
- DB_DRIVER=${DB_DRIVER}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DB_NAME=${DB_NAME}
- DB_PORT=${DB_PORT}
- POSTGRES_URL=${POSTGRES_URL}
# - PORT=${PORT}

postgres:
image: postgres:latest
Expand All @@ -21,16 +27,13 @@ services:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
- DATABASE_HOST=${DB_HOST}
- DATABASE_HOST=${DB_HOST}
ports:
- '5432:5432'
- 5432:5432
volumes:
- ./docker-compose/postgres:/var/lib/postgresql/data
- ./docker-compose/postgres:/var/lib/postgresql
networks:
- fullstack

volumes:
database_postgres:
- fullstack

# Networks to be created to facilitate communication between containers
networks:
Expand Down
18 changes: 9 additions & 9 deletions k8s-deployments/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ spec:
spec: # Spec for the container which will run in the Pod
imagePullSecrets:
- name: dockerconfigjson-github-com
# initContainers:
# - name: check-db-ready
# image: postgres:latest
# command: ['sh', '-c',
# 'until pg_isready -h $DB_HOST -p $DB_PORT;
# do echo waiting for database; sleep 2; done;']
# envFrom:
# - secretRef:
# name: app-secret
initContainers:
- name: check-db-ready
image: postgres:latest
command: ['sh', '-c',
'until pg_isready -h $DB_HOST -p $DB_PORT;
do echo waiting for database; sleep 2; done;']
envFrom:
- secretRef:
name: app-secret
containers:
- name: go-todo-api
image: ghcr.io/harshsinghvi/golang-postgres-kubernetes:latest
Expand Down

0 comments on commit 6b27af3

Please sign in to comment.