Skip to content

Commit 6b27af3

Browse files
committed
Update Update controller to update text field too
1 parent 582d42b commit 6b27af3

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
```bash
5555
kubectl rollout restart deployment/name # to update image
5656
kubectl get ingress # ingress exposed url
57+
kubectl port-forward statefulset.apps/postgres 5432:5432
5758
kubectl exec --stdin --tty pod/postgres-0 -- /bin/bash
5859
aws eks update-kubeconfig --region ap-south-1 --name cluster
5960
```
@@ -64,9 +65,7 @@ aws eks update-kubeconfig --region ap-south-1 --name cluster
6465

6566
```bash
6667
docker buildx build --platform=linux/amd64 -t golang-postgres-kubernetes .
67-
6868
docker tag golang-postgres-kubernetes ghcr.io/harshsinghvi/golang-postgres-kubernetes:latest
69-
7069
docker push ghcr.io/harshsinghvi/golang-postgres-kubernetes:latest
7170
```
7271

controllers/controllers.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,15 @@ func EditTodo(c *gin.Context) {
9292
todoId := c.Param("id")
9393
var todo models.Todo
9494
c.BindJSON(&todo)
95-
completed := todo.Completed
96-
_, err := database.Connection.Model(&models.Todo{}).Set("completed = ?", completed).Where("id = ?", todoId).Update()
95+
96+
querry := database.Connection.Model(&models.Todo{}).Set("completed = ?", todo.Completed)
97+
98+
if todo.Text != "" {
99+
querry.Set("text = ?", todo.Text)
100+
}
101+
102+
res, err := querry.Where("id = ?", todoId).Update()
103+
97104
if err != nil {
98105
log.Printf("Error, Reason: %v\n", err)
99106
c.JSON(http.StatusInternalServerError, gin.H{
@@ -102,6 +109,16 @@ func EditTodo(c *gin.Context) {
102109
})
103110
return
104111
}
112+
113+
if res.RowsAffected() == 0 {
114+
log.Printf("Error while update todo, Reason: \n")
115+
c.JSON(http.StatusNotFound, gin.H{
116+
"status": http.StatusNotFound,
117+
"message": "Todo not found",
118+
})
119+
return
120+
}
121+
105122
c.JSON(http.StatusOK, gin.H{
106123
"status": 200,
107124
"message": "Todo Edited Successfully",

docker-compose.yml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ services:
1111
networks:
1212
- fullstack
1313
environment:
14-
- PORT=${PORT}
15-
- POSTGRES_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=disable
14+
- DB_HOST=${DB_HOST}
15+
- DB_DRIVER=${DB_DRIVER}
16+
- DB_USER=${DB_USER}
17+
- DB_PASSWORD=${DB_PASSWORD}
18+
- DB_NAME=${DB_NAME}
19+
- DB_PORT=${DB_PORT}
20+
- POSTGRES_URL=${POSTGRES_URL}
21+
# - PORT=${PORT}
1622

1723
postgres:
1824
image: postgres:latest
@@ -21,16 +27,13 @@ services:
2127
- POSTGRES_USER=${DB_USER}
2228
- POSTGRES_PASSWORD=${DB_PASSWORD}
2329
- POSTGRES_DB=${DB_NAME}
24-
- DATABASE_HOST=${DB_HOST}
30+
- DATABASE_HOST=${DB_HOST}
2531
ports:
26-
- '5432:5432'
32+
- 5432:5432
2733
volumes:
28-
- ./docker-compose/postgres:/var/lib/postgresql/data
34+
- ./docker-compose/postgres:/var/lib/postgresql
2935
networks:
30-
- fullstack
31-
32-
volumes:
33-
database_postgres:
36+
- fullstack
3437

3538
# Networks to be created to facilitate communication between containers
3639
networks:

k8s-deployments/deployment.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ spec:
1515
spec: # Spec for the container which will run in the Pod
1616
imagePullSecrets:
1717
- name: dockerconfigjson-github-com
18-
# initContainers:
19-
# - name: check-db-ready
20-
# image: postgres:latest
21-
# command: ['sh', '-c',
22-
# 'until pg_isready -h $DB_HOST -p $DB_PORT;
23-
# do echo waiting for database; sleep 2; done;']
24-
# envFrom:
25-
# - secretRef:
26-
# name: app-secret
18+
initContainers:
19+
- name: check-db-ready
20+
image: postgres:latest
21+
command: ['sh', '-c',
22+
'until pg_isready -h $DB_HOST -p $DB_PORT;
23+
do echo waiting for database; sleep 2; done;']
24+
envFrom:
25+
- secretRef:
26+
name: app-secret
2727
containers:
2828
- name: go-todo-api
2929
image: ghcr.io/harshsinghvi/golang-postgres-kubernetes:latest

0 commit comments

Comments
 (0)