Skip to content

Commit

Permalink
fix: fixed delete todo response and button hover frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Molaryy committed Nov 19, 2023
1 parent 03c77eb commit 04b6818
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 23 deletions.
8 changes: 5 additions & 3 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require github.com/gin-gonic/gin v1.9.1
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -20,6 +21,7 @@ require (
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
Expand All @@ -31,10 +33,10 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/postgres v1.5.3 // indirect
Expand Down
10 changes: 10 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down Expand Up @@ -41,6 +43,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down Expand Up @@ -83,6 +87,8 @@ golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -91,10 +97,14 @@ golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
Expand Down
2 changes: 1 addition & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ func main() {

r := gin.Default()
router(r)
utils.CheckError(r.Run())
utils.CheckError(r.Run(":8080"))
}
13 changes: 12 additions & 1 deletion backend/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ import (
"net/http"
)

type RegisterInput struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}

func AuthController(c *gin.Context) {
var input RegisterInput

if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "do ypu really wanna authenticate",
"message": "You just registered",
})
}
32 changes: 21 additions & 11 deletions backend/models/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ func (h Handler) DeleteTodo(c *gin.Context) {

if result := h.DB.First(&todo, todoId); result.Error != nil {
fmt.Printf("Todo with id: %v could't be found\n", todoId)
statusCode = http.StatusNotFound
}
h.DB.Delete(&todo)
c.JSON(statusCode, gin.H{
"message": fmt.Sprintf("Successfully deleted with id: %v", todoId),
})
if statusCode == http.StatusAccepted {
c.JSON(statusCode, gin.H{
"todo": todo,
})
} else {
c.JSON(statusCode, gin.H{
"message": fmt.Sprintf("Todo with id %v couldn't be found\n", todoId),
})
}
}

func (h Handler) GetAllTodos(c *gin.Context) {
Expand All @@ -51,12 +58,17 @@ func (h Handler) GetAllTodos(c *gin.Context) {

if result := h.DB.Find(&todos); result.Error != nil || len(todos) == 0 {
statusCode = http.StatusNotFound
fmt.Println("Couldn't find todos")
}

c.JSON(statusCode, gin.H{
"todos": todos,
})
if statusCode == http.StatusAccepted {
c.JSON(statusCode, gin.H{
"todo": todos,
})
} else {
c.JSON(statusCode, gin.H{
"message": fmt.Sprintf("There aren't any todos yet"),
})
}
}

func (h Handler) GetTodo(c *gin.Context) {
Expand All @@ -65,7 +77,6 @@ func (h Handler) GetTodo(c *gin.Context) {
statusCode := http.StatusAccepted

if result := h.DB.First(&todo, todoId); result.Error != nil {
fmt.Printf("Todo with id: %v could't be found\n", todoId)
statusCode = http.StatusNotFound
}

Expand All @@ -75,7 +86,7 @@ func (h Handler) GetTodo(c *gin.Context) {
})
} else {
c.JSON(statusCode, gin.H{
"message": fmt.Sprintf("Todo with id %v could't be found\n", todoId),
"message": fmt.Sprintf("Todo with id %v couldn't be found\n", todoId),
})
}
}
Expand Down Expand Up @@ -117,8 +128,7 @@ func (h Handler) UpdateTodo(c *gin.Context) {
return
}
updateTodoFields(&storedTodo, updatedTodo)
// h.DB.Save(&storedTodo)
fmt.Println(storedTodo)
h.DB.Save(&storedTodo)
c.JSON(statusCode, gin.H{
"todo": storedTodo,
})
Expand Down
21 changes: 21 additions & 0 deletions backend/models/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package models

import "gorm.io/gorm"

type User struct {
Id int `json:"id" gorm:"primary_key;"`
Username string `json:"username" gorm:"size:255;not null;unique"`
Password string `json:"password" gorm:"size:255;not null;"`
Email string `json:"email" gorm:"not null;unique;"`
}

func (u *User) SaveUser() (*User, error) {
var DB *gorm.DB
var err error

err = DB.Create(&u).Error
if err != nil {
return &User{}, err
}
return u, nil
}
8 changes: 6 additions & 2 deletions backend/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ func todosHandler(r *gin.Engine) {
r.PATCH("/todo/:id", handler.UpdateTodo)
}

func router(r *gin.Engine) {
func authHandler(r *gin.Engine) {
r.POST("/register", middlewares.AuthController)

}

r.POST("/auth", middlewares.AuthController)
func router(r *gin.Engine) {
authHandler(r)
todosHandler(r)
r.NoRoute(RouteNotFound)
}
8 changes: 4 additions & 4 deletions frontend/src/components/Todos/GetTodo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ const GetTodoButton = () => {
className={'get-todos-container'}
style={{ backgroundColor: bgColorButton, color: colorText }}
onMouseOver={() => {
setBgColorButton('rgb(26,62,176)');
setColorText('rgb(26,62,176)');
setBgColorButton('rgb(49,45,45)');
setColorText('rgb(239,94,94)');
}}
onMouseLeave={() => {
setBgColorButton('rgb(26,62,176)');
setColorText('rgb(26,62,176)');
setBgColorButton('rgb(239,94,94)');
setColorText('rgb(49,45,45)');
}}
>
<h3>Create todo</h3>
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/Todos/ListTodosContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const ListTodosContainer = ({ todos }: { todos: TodoList[] }) => {
></div>
<div className={'todos-list-text'}>
{todos.map((todo, index) => {
console.log(settingsOpened);
return settingsOpened[index] ? (
<div
key={index}
Expand Down

0 comments on commit 04b6818

Please sign in to comment.