Skip to content

Commit a337910

Browse files
committed
find user by id and email
1 parent 5d3cbb2 commit a337910

File tree

8 files changed

+136
-9
lines changed

8 files changed

+136
-9
lines changed

.http

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44
### GET users
55
GET {{apiurl}}/users HTTP/1.1
66

7+
### GET user by ID
8+
GET {{apiurl}}/getUserById/66777d1b9242370bf4575b31 HTTP/1.1
9+
10+
### GET user by EMAIL
11+
GET {{apiurl}}/getUserByEmail/test@test.com HTTP/1.1
12+
713
### POST users
814
POST {{apiurl}}/createUser HTTP/1.1
915
Content-Type: application/json
1016
Authorization: Bearer ACCESS-TOKEN
1117

1218
{
13-
"email":"test@test.com",
19+
"email":"test2@test.com",
1420
"password":"test@123",
15-
"name":"test",
16-
"age":19
21+
"name":"test2",
22+
"age":20
1723
}
1824

1925
### GET categories

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/gin-contrib/sse v0.1.0 // indirect
2323
github.com/goccy/go-json v0.10.2 // indirect
2424
github.com/golang/snappy v0.0.1 // indirect
25+
github.com/google/uuid v1.6.0 // indirect
2526
github.com/json-iterator/go v1.1.12 // indirect
2627
github.com/klauspost/compress v1.13.6 // indirect
2728
github.com/klauspost/cpuid/v2 v2.2.7 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
3030
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
3131
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3232
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
33+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
34+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3335
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
3436
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
3537
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=

src/controller/find_user.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
11
package controller
22

3-
import "github.com/gin-gonic/gin"
3+
import (
4+
"net/http"
5+
"net/mail"
46

5-
func (uc *userControllerInterface) FindUserByID(c *gin.Context) {}
7+
"github.com/gin-gonic/gin"
8+
"github.com/murillolamego/golang-basic/src/config/rest_err"
9+
"github.com/murillolamego/golang-basic/src/view"
10+
"go.mongodb.org/mongo-driver/bson/primitive"
11+
)
612

7-
func (uc *userControllerInterface) FindUserByEmail(c *gin.Context) {}
13+
func (uc *userControllerInterface) FindUserByID(c *gin.Context) {
14+
userId := c.Param("userId")
15+
16+
if _, err := primitive.ObjectIDFromHex(userId); err != nil {
17+
errorMessage := rest_err.NewBadRequestError("UserID is not a valid id")
18+
19+
c.JSON(errorMessage.Code, errorMessage.Message)
20+
return
21+
}
22+
23+
userDomain, err := uc.service.FindUserByID(userId)
24+
if err != nil {
25+
c.JSON(err.Code, err)
26+
return
27+
}
28+
29+
c.JSON(http.StatusOK, view.ConvertDomainToResponse(userDomain))
30+
}
31+
32+
func (uc *userControllerInterface) FindUserByEmail(c *gin.Context) {
33+
email := c.Param("userEmail")
34+
35+
if _, err := mail.ParseAddress(email); err != nil {
36+
errorMessage := rest_err.NewBadRequestError("Email is not valid")
37+
38+
c.JSON(errorMessage.Code, errorMessage.Message)
39+
return
40+
}
41+
42+
userDomain, err := uc.service.FindUserByEmail(email)
43+
if err != nil {
44+
c.JSON(err.Code, err)
45+
return
46+
}
47+
48+
c.JSON(http.StatusOK, view.ConvertDomainToResponse(userDomain))
49+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/murillolamego/golang-basic/src/config/logger"
9+
"github.com/murillolamego/golang-basic/src/config/rest_err"
10+
"github.com/murillolamego/golang-basic/src/model"
11+
"github.com/murillolamego/golang-basic/src/model/repository/entity"
12+
"github.com/murillolamego/golang-basic/src/model/repository/entity/converter"
13+
"go.mongodb.org/mongo-driver/bson"
14+
"go.mongodb.org/mongo-driver/bson/primitive"
15+
"go.mongodb.org/mongo-driver/mongo"
16+
)
17+
18+
func (ur *userRepository) FindUserByID(id string) (model.UserDomainInterface, *rest_err.RestErr) {
19+
logger.Info("init findUserByID repository")
20+
21+
collection_name := os.Getenv(MONGODB_USER_COLLECTION)
22+
23+
collection := ur.databaseConnection.Collection(collection_name)
24+
25+
userEntity := &entity.UserEntity{}
26+
27+
objectId, _ := primitive.ObjectIDFromHex((id))
28+
filter := bson.D{{Key: "_id", Value: objectId}}
29+
err := collection.FindOne(context.Background(), filter).Decode(userEntity)
30+
31+
if err != nil {
32+
if err == mongo.ErrNoDocuments {
33+
errorMessage := fmt.Sprintf("User with ID %s not found", id)
34+
35+
return nil, rest_err.NewNotFoundErrorfunc(errorMessage)
36+
}
37+
38+
return nil, rest_err.NewInternalServerErrorfunc("Error trying to find user by ID")
39+
}
40+
41+
return converter.ConvertEntityToDomain(*userEntity), nil
42+
}
43+
44+
func (ur *userRepository) FindUserByEmail(email string) (model.UserDomainInterface, *rest_err.RestErr) {
45+
logger.Info("init findUserByEmail repository")
46+
47+
collection_name := os.Getenv(MONGODB_USER_COLLECTION)
48+
49+
collection := ur.databaseConnection.Collection(collection_name)
50+
51+
userEntity := &entity.UserEntity{}
52+
53+
filter := bson.D{{Key: "email", Value: email}}
54+
err := collection.FindOne(context.Background(), filter).Decode(userEntity)
55+
56+
if err != nil {
57+
if err == mongo.ErrNoDocuments {
58+
errorMessage := fmt.Sprintf("User with email %s not found", email)
59+
60+
return nil, rest_err.NewNotFoundErrorfunc(errorMessage)
61+
}
62+
63+
return nil, rest_err.NewInternalServerErrorfunc("Error trying to find user by email")
64+
}
65+
66+
return converter.ConvertEntityToDomain(*userEntity), nil
67+
}

src/model/repository/user_repository.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ type UserRepository interface {
2222
CreateUser(
2323
userDomain model.UserDomainInterface,
2424
) (model.UserDomainInterface, *rest_err.RestErr)
25+
26+
FindUserByID(id string) (model.UserDomainInterface, *rest_err.RestErr)
27+
28+
FindUserByEmail(email string) (model.UserDomainInterface, *rest_err.RestErr)
2529
}

src/model/service/find_user.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
"github.com/murillolamego/golang-basic/src/model"
66
)
77

8-
func (ud *userDomainService) FindUser(userId string) (model.UserDomainInterface, *rest_err.RestErr) {
9-
return nil, nil
8+
func (ud *userDomainService) FindUserByID(id string) (model.UserDomainInterface, *rest_err.RestErr) {
9+
return ud.userRepository.FindUserByID(id)
10+
}
11+
12+
func (ud *userDomainService) FindUserByEmail(email string) (model.UserDomainInterface, *rest_err.RestErr) {
13+
return ud.userRepository.FindUserByEmail(email)
1014
}

src/model/service/user_interface.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ type userDomainService struct {
1616

1717
type UserDomainService interface {
1818
CreateUser(model.UserDomainInterface) (model.UserDomainInterface, *rest_err.RestErr)
19-
FindUser(string) (model.UserDomainInterface, *rest_err.RestErr)
19+
FindUserByID(id string) (model.UserDomainInterface, *rest_err.RestErr)
20+
FindUserByEmail(email string) (model.UserDomainInterface, *rest_err.RestErr)
2021
UpdateUser(string, model.UserDomainInterface) (model.UserDomainInterface, *rest_err.RestErr)
2122
DeleteUser(string) *rest_err.RestErr
2223
}

0 commit comments

Comments
 (0)