Skip to content

Commit 9b7f69c

Browse files
authored
Merge pull request #2 from scarlettmiss/register_and_get_users
users api
2 parents da19d29 + 2e8a5e8 commit 9b7f69c

File tree

26 files changed

+635
-189
lines changed

26 files changed

+635
-189
lines changed

api/api.go

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
"github.com/gin-gonic/gin"
6+
"github.com/google/uuid"
7+
"github.com/samber/lo"
8+
"github.com/scarlettmiss/bestPal/application"
9+
"github.com/scarlettmiss/bestPal/application/domain/user"
10+
"github.com/scarlettmiss/bestPal/cmd/server/types"
11+
"github.com/scarlettmiss/bestPal/converters"
12+
"github.com/scarlettmiss/bestPal/middlewares"
13+
"github.com/scarlettmiss/bestPal/utils"
14+
"net/http"
15+
)
16+
17+
type API struct {
18+
*gin.Engine
19+
app *application.Application
20+
}
21+
22+
func New(application *application.Application) *API {
23+
api := &API{
24+
Engine: gin.Default(),
25+
app: application,
26+
}
27+
28+
api.NoRoute(func(ctx *gin.Context) { ctx.Status(http.StatusNotFound) })
29+
30+
api.POST("/api/auth/register", api.register)
31+
api.POST("/api/auth/login", api.login)
32+
33+
protected := api.Group("/").Use(middlewares.Auth())
34+
protected.PATCH("/api/user", api.updateUser)
35+
protected.DELETE("/api/user", api.deleteUser)
36+
protected.GET("/api/user/:id", api.user)
37+
protected.GET("/api/user", api.user)
38+
protected.GET("/api/users", api.users)
39+
40+
return api
41+
}
42+
43+
func (api *API) register(c *gin.Context) {
44+
var requestBody types.UserCreateRequest
45+
46+
err := c.ShouldBindJSON(&requestBody)
47+
if err != nil {
48+
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
49+
return
50+
}
51+
52+
u, err := converters.UserCreateRequestToUser(requestBody)
53+
if err != nil {
54+
c.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
55+
return
56+
}
57+
58+
u, err = api.app.CreateUser(u)
59+
if err != nil {
60+
if err == user.ErrMailExists {
61+
c.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
62+
} else {
63+
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
64+
}
65+
return
66+
}
67+
68+
token, err := api.app.UserToken(u)
69+
if err != nil {
70+
c.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
71+
return
72+
}
73+
74+
c.JSON(http.StatusCreated, gin.H{"user": converters.UserToResponse(u), "token": token})
75+
fmt.Println(u)
76+
77+
}
78+
79+
func (api *API) login(c *gin.Context) {
80+
var requestBody types.LoginRequest
81+
82+
err := c.ShouldBindJSON(&requestBody)
83+
if err != nil {
84+
c.Status(http.StatusBadRequest)
85+
return
86+
}
87+
u, err := api.app.Authenticate(requestBody.Email, requestBody.Password)
88+
if err != nil {
89+
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
90+
return
91+
}
92+
93+
token, err := api.app.UserToken(u)
94+
if err != nil {
95+
c.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
96+
return
97+
}
98+
99+
c.JSON(http.StatusCreated, gin.H{"user": converters.UserToResponse(u), "token": token})
100+
101+
}
102+
103+
func (api *API) users(c *gin.Context) {
104+
users := api.app.Users()
105+
106+
usersResp := lo.MapValues(users, func(u user.User, _ uuid.UUID) types.UserResponse {
107+
return converters.UserToResponse(u)
108+
})
109+
110+
c.JSON(http.StatusOK, usersResp)
111+
}
112+
113+
func (api *API) user(c *gin.Context) {
114+
id := c.Param("id")
115+
116+
var ok bool
117+
if id == "" {
118+
idParam, _ := c.Get("UserId")
119+
// Convert UserId to string if it's not already.
120+
id, ok = idParam.(string)
121+
if !ok {
122+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
123+
return
124+
}
125+
}
126+
127+
//parse
128+
uId, err := uuid.Parse(id)
129+
if err != nil {
130+
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
131+
return
132+
}
133+
134+
u, err := api.app.User(uId)
135+
if err != nil {
136+
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
137+
return
138+
}
139+
140+
c.JSON(http.StatusOK, converters.UserToResponse(u))
141+
}
142+
143+
func (api *API) deleteUser(c *gin.Context) {
144+
idParam, _ := c.Get("UserId")
145+
// Convert UserId to string if it's not already.
146+
id, ok := idParam.(string)
147+
if !ok {
148+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
149+
return
150+
}
151+
152+
//parse
153+
uId, err := uuid.Parse(id)
154+
if err != nil {
155+
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
156+
return
157+
}
158+
159+
err = api.app.DeleteUser(uId)
160+
if err != nil {
161+
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
162+
return
163+
}
164+
165+
c.JSON(http.StatusOK, gin.H{"message": "user deleted"})
166+
}
167+
168+
func (api *API) updateUser(c *gin.Context) {
169+
id, _ := c.Get("UserId")
170+
// Convert UserId to string if it's not already.
171+
idString, ok := id.(string)
172+
if !ok {
173+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
174+
return
175+
}
176+
177+
uId, err := uuid.Parse(idString)
178+
if err != nil {
179+
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
180+
return
181+
}
182+
183+
var requestBody types.UserUpdateRequest
184+
err = c.ShouldBindJSON(&requestBody)
185+
if err != nil {
186+
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
187+
return
188+
}
189+
190+
u, err := api.app.User(uId)
191+
if err != nil {
192+
c.JSON(http.StatusNotFound, utils.ErrorResponse(err))
193+
return
194+
}
195+
196+
u = converters.UserUpdateRequestToUser(requestBody, u)
197+
198+
u, err = api.app.UpdateUser(u)
199+
if err != nil {
200+
if err == user.ErrMailExists {
201+
c.JSON(http.StatusBadRequest, utils.ErrorResponse(err))
202+
} else {
203+
c.JSON(http.StatusInternalServerError, utils.ErrorResponse(err))
204+
}
205+
return
206+
}
207+
208+
c.JSON(http.StatusOK, converters.UserToResponse(u))
209+
}

api/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package config
2+
3+
const Host = ""
4+
const Port = "8080"

application/application.go

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package application
22

33
import (
4+
"github.com/google/uuid"
5+
"github.com/scarlettmiss/bestPal/application/domain/user"
6+
jwtService "github.com/scarlettmiss/bestPal/application/services/jwtService"
47
petService "github.com/scarlettmiss/bestPal/application/services/petService"
58
treatmentService "github.com/scarlettmiss/bestPal/application/services/treatmentService"
69
userService "github.com/scarlettmiss/bestPal/application/services/userService"
7-
"github.com/scarlettmiss/bestPal/cmd/server/types"
810
)
911

1012
/*
@@ -29,7 +31,58 @@ func New(opts Options) *Application {
2931
return &app
3032
}
3133

32-
func (a Application) createUser(u types.Account) {
33-
a.userService.CreateUser(u)
34+
func (a *Application) CreateUser(u user.User) (user.User, error) {
35+
err := a.CheckEmail(u.Email, u.Id)
36+
if err != nil {
37+
return user.Nil, err
38+
}
3439

40+
return a.userService.CreateUser(u)
41+
}
42+
43+
func (a *Application) UserToken(u user.User) (string, error) {
44+
return jwtService.GenerateJWT(u.Id, u.UserType)
45+
}
46+
47+
func (a *Application) CheckEmail(email string, id uuid.UUID) error {
48+
u, ok := a.userService.UserByEmail(email)
49+
50+
if !ok {
51+
return nil
52+
}
53+
54+
if u.Id == id {
55+
return nil
56+
}
57+
58+
return user.ErrMailExists
59+
}
60+
61+
func (a *Application) UpdateUser(u user.User) (user.User, error) {
62+
err := a.CheckEmail(u.Email, u.Id)
63+
if err != nil {
64+
return user.Nil, err
65+
}
66+
return a.userService.UpdateUser(u)
67+
}
68+
69+
func (a *Application) Users() map[uuid.UUID]user.User {
70+
return a.userService.Users()
71+
}
72+
73+
func (a *Application) User(id uuid.UUID) (user.User, error) {
74+
return a.userService.User(id)
75+
}
76+
77+
func (a *Application) DeleteUser(id uuid.UUID) error {
78+
return a.userService.DeleteUser(id)
79+
}
80+
81+
func (a *Application) Authenticate(email string, password string) (user.User, error) {
82+
u, err := a.userService.Authenticate(email, password)
83+
if err != nil {
84+
return user.Nil, err
85+
}
86+
87+
return u, nil
3588
}

application/domain/base/base.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

application/domain/base/interfaces.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

application/domain/pet/pet.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package pet
22

33
import (
4-
"github.com/scarlettmiss/bestPal/application/domain/base"
4+
"github.com/google/uuid"
55
"time"
66
)
77

8-
type BehaviorType string
9-
10-
const (
11-
Aggressive BehaviorType = "aggressive"
12-
Friendly BehaviorType = "friendly"
13-
)
14-
158
type Pet struct {
16-
base.Base
9+
Id uuid.UUID
10+
CreatedAt time.Time
11+
UpdatedAt time.Time
12+
Deleted bool
1713
Name string
1814
DateOfBirth string
1915
Sex string
@@ -22,7 +18,7 @@ type Pet struct {
2218
Description string
2319
Pedigree string
2420
Microchip string
25-
Behavior BehaviorType
21+
Friendly bool
2622
WeightHistory map[time.Time]float64
2723
OwnerId string
2824
}

0 commit comments

Comments
 (0)