Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit73ce659

Browse files
committed
updated
1 parentb66ef28 commit73ce659

File tree

12 files changed

+43
-46
lines changed

12 files changed

+43
-46
lines changed

‎cmd/server/main.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var (
3838
AuthController controllers.AuthController
3939
AuthRouteController routes.AuthRouteController
4040

41+
// 👇 Add the Post Service, Controllers and Routes
4142
postService services.PostService
4243
PostController controllers.PostController
4344
postCollection*mongo.Collection
@@ -92,6 +93,7 @@ func init() {
9293
UserController=controllers.NewUserController(userService)
9394
UserRouteController=routes.NewRouteUserController(UserController)
9495

96+
// 👇 Add the Post Service, Controllers and Routes
9597
postCollection=mongoclient.Database("golang_mongodb").Collection("posts")
9698
postService=services.NewPostService(postCollection,ctx)
9799
PostController=controllers.NewPostController(postService)
@@ -164,6 +166,7 @@ func startGinServer(config config.Config) {
164166

165167
AuthRouteController.AuthRoute(router,userService)
166168
UserRouteController.UserRoute(router,userService)
169+
// 👇 Evoke the PostRoute
167170
PostRouteController.PostRoute(router)
168171
log.Fatal(server.Run(":"+config.Port))
169172
}

‎controllers/auth.controller.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (ac *AuthController) SignUpUser(ctx *gin.Context) {
6363

6464
verificationCode:=utils.Encode(code)
6565

66-
updateData:=&models.UpdateInput{
66+
updateData:=&models.UserUpdateInput{
6767
VerificationCode:verificationCode,
6868
}
6969

@@ -205,8 +205,6 @@ func (ac *AuthController) VerifyEmail(ctx *gin.Context) {
205205
return
206206
}
207207

208-
fmt.Println(result)
209-
210208
ctx.JSON(http.StatusOK, gin.H{"status":"success","message":"Email verified successfully"})
211209

212210
}

‎controllers/post.controller.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func (pc *PostController) FindPostById(ctx *gin.Context) {
6969
post,err:=pc.postService.FindPostById(postId)
7070

7171
iferr!=nil {
72+
ifstrings.Contains(err.Error(),"Id exists") {
73+
ctx.JSON(http.StatusNotFound, gin.H{"status":"fail","message":err.Error()})
74+
return
75+
}
7276
ctx.JSON(http.StatusBadGateway, gin.H{"status":"fail","message":err.Error()})
7377
return
7478
}

‎controllers/user.controller.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func NewUserController(userService services.UserService) UserController {
1717
}
1818

1919
func (uc*UserController)GetMe(ctx*gin.Context) {
20-
currentUser:=ctx.MustGet("currentUser").(*models.DBResponse)
20+
currentUser:=ctx.MustGet("currentUser").(*models.UserDBResponse)
2121

22-
ctx.JSON(http.StatusOK, gin.H{"status":"success","data": gin.H{"user":models.FilteredResponse(currentUser)}})
22+
ctx.JSON(http.StatusOK, gin.H{"status":"success","data": gin.H{"user":models.FilteredUserResponse(currentUser)}})
2323
}

‎gapi/rpc_signup_user.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (authServer *AuthServer) SignUpUser(ctx context.Context, req *pb.SignUpUser
3939

4040
verificationCode:=utils.Encode(code)
4141

42-
updateData:=&models.UpdateInput{
42+
updateData:=&models.UserUpdateInput{
4343
VerificationCode:verificationCode,
4444
}
4545

‎models/user.model.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type SignInInput struct {
2727
Passwordstring`json:"password" bson:"password" binding:"required"`
2828
}
2929

30-
// 👈DBResponse struct
31-
typeDBResponsestruct {
30+
// 👈UserDBResponse struct
31+
typeUserDBResponsestruct {
3232
ID primitive.ObjectID`json:"id" bson:"_id"`
3333
Namestring`json:"name" bson:"name"`
3434
Emailstring`json:"email" bson:"email"`
@@ -43,7 +43,7 @@ type DBResponse struct {
4343
UpdatedAt time.Time`json:"updated_at" bson:"updated_at"`
4444
}
4545

46-
typeUpdateInputstruct {
46+
typeUserUpdateInputstruct {
4747
Namestring`json:"name,omitempty" bson:"name,omitempty"`
4848
Emailstring`json:"email,omitempty" bson:"email,omitempty"`
4949
Passwordstring`json:"password,omitempty" bson:"password,omitempty"`
@@ -77,7 +77,7 @@ type ResetPasswordInput struct {
7777
PasswordConfirmstring`json:"passwordConfirm,omitempty" bson:"passwordConfirm,omitempty"`
7878
}
7979

80-
funcFilteredResponse(user*DBResponse)UserResponse {
80+
funcFilteredUserResponse(user*UserDBResponse)UserResponse {
8181
returnUserResponse{
8282
ID:user.ID,
8383
Email:user.Email,

‎services/auth.service.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package services
33
import"github.com/wpcodevo/golang-mongodb/models"
44

55
typeAuthServiceinterface {
6-
SignUpUser(*models.SignUpInput) (*models.DBResponse,error)
7-
SignInUser(*models.SignInInput) (*models.DBResponse,error)
6+
SignUpUser(*models.SignUpInput) (*models.UserDBResponse,error)
7+
SignInUser(*models.SignInInput) (*models.UserDBResponse,error)
88
}

‎services/auth.service.impl.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func NewAuthService(collection *mongo.Collection, ctx context.Context) AuthServi
2222
return&AuthServiceImpl{collection,ctx}
2323
}
2424

25-
func (uc*AuthServiceImpl)SignUpUser(user*models.SignUpInput) (*models.DBResponse,error) {
25+
func (uc*AuthServiceImpl)SignUpUser(user*models.SignUpInput) (*models.UserDBResponse,error) {
2626
user.CreatedAt=time.Now()
2727
user.UpdatedAt=user.CreatedAt
2828
user.Email=strings.ToLower(user.Email)
@@ -50,7 +50,7 @@ func (uc *AuthServiceImpl) SignUpUser(user *models.SignUpInput) (*models.DBRespo
5050
returnnil,errors.New("could not create index for email")
5151
}
5252

53-
varnewUser*models.DBResponse
53+
varnewUser*models.UserDBResponse
5454
query:= bson.M{"_id":res.InsertedID}
5555

5656
err=uc.collection.FindOne(uc.ctx,query).Decode(&newUser)
@@ -61,6 +61,6 @@ func (uc *AuthServiceImpl) SignUpUser(user *models.SignUpInput) (*models.DBRespo
6161
returnnewUser,nil
6262
}
6363

64-
func (uc*AuthServiceImpl)SignInUser(*models.SignInInput) (*models.DBResponse,error) {
64+
func (uc*AuthServiceImpl)SignInUser(*models.SignInInput) (*models.UserDBResponse,error) {
6565
returnnil,nil
6666
}

‎services/post.service.impl.go‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,12 @@ func (p *PostServiceImpl) UpdatePost(id string, data *models.UpdatePost) (*model
6161
obId,_:=primitive.ObjectIDFromHex(id)
6262
query:= bson.D{{Key:"_id",Value:obId}}
6363
update:= bson.D{{Key:"$set",Value:doc}}
64-
res,err:=p.postCollection.UpdateOne(p.ctx,query,update)
65-
iferr!=nil {
66-
returnnil,err
67-
}
68-
69-
ifres.MatchedCount==0 {
70-
returnnil,errors.New("no post with that Id exists")
71-
}
64+
res:=p.postCollection.FindOneAndUpdate(p.ctx,query,update,options.FindOneAndUpdate().SetReturnDocument(1))
7265

7366
varupdatedPost*models.DBPost
7467

75-
iferr=p.postCollection.FindOne(p.ctx,query).Decode(&updatedPost);err!=nil {
76-
returnnil,err
68+
iferr:=res.Decode(&updatedPost);err!=nil {
69+
returnnil,errors.New("no post with that Id exists")
7770
}
7871

7972
returnupdatedPost,nil
@@ -115,7 +108,7 @@ func (p *PostServiceImpl) FindPosts(page int, limit int) ([]*models.DBPost, erro
115108

116109
varposts []*models.DBPost
117110

118-
ifcursor.Next(p.ctx) {
111+
forcursor.Next(p.ctx) {
119112
post:=&models.DBPost{}
120113
err:=cursor.Decode(post)
121114

‎services/user.service.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package services
33
import"github.com/wpcodevo/golang-mongodb/models"
44

55
typeUserServiceinterface {
6-
FindUserById(idstring) (*models.DBResponse,error)
7-
FindUserByEmail(emailstring) (*models.DBResponse,error)
8-
UpdateUserById(idstring,data*models.UpdateInput) (*models.DBResponse,error)
6+
FindUserById(idstring) (*models.UserDBResponse,error)
7+
FindUserByEmail(emailstring) (*models.UserDBResponse,error)
8+
UpdateUserById(idstring,data*models.UserUpdateInput) (*models.UserDBResponse,error)
99
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp