Skip to content

Commit

Permalink
get profile
Browse files Browse the repository at this point in the history
  • Loading branch information
donaderoyan committed Jun 3, 2024
1 parent d49efea commit 7119973
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 57 deletions.
8 changes: 8 additions & 0 deletions controllers/user/profile/get-service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package profile

import model "github.com/donaderoyan/talentgrowth-be/models"

func (s *service) GetProfileService(userID string) (*model.User, error) {
result, err := s.repository.GetProfile(userID)
return result, err
}
12 changes: 0 additions & 12 deletions controllers/user/profile/patch-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ import (
"go.mongodb.org/mongo-driver/bson"
)

type Service interface {
UpdateProfileService(userID string, input *UpdateProfileInput) (*model.User, error)
}

type service struct {
repository Repository
}

func NewProfileService(repository Repository) *service {
return &service{repository: repository}
}

func (s *service) UpdateProfileService(userID string, input *UpdateProfileInput) (*model.User, error) {
// Update user profile
// Ensure correct field names are used for MongoDB document
Expand Down
14 changes: 1 addition & 13 deletions controllers/user/profile/put-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
)

type PutService interface {
PutProfileService(userID string, input *UpdateProfileInput) (*model.User, error)
}

type putservice struct {
repository Repository
}

func NewPutProfileService(repository Repository) *putservice {
return &putservice{repository: repository}
}

func (s *putservice) PutProfileService(userID string, input *UpdateProfileInput) (*model.User, error) {
func (s *service) PutProfileService(userID string, input *UpdateProfileInput) (*model.User, error) {
// Update user profile
// Ensure correct field names are used for MongoDB document
var parsedBirthday time.Time
Expand Down
42 changes: 42 additions & 0 deletions controllers/user/profile/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type Repository interface {
UpdateProfile(userID string, updates bson.M) (*model.User, error)
GetProfile(userID string) (*model.User, error)
}

type repository struct {
Expand All @@ -31,6 +32,14 @@ func (e *UserProfileUpdateError) Error() string {
return fmt.Sprintf("Profile update error: %s - %s", e.Code, e.Message)
}

type GetUserProfileError struct {
*util.BaseError
}

func (e *GetUserProfileError) Error() string {
return fmt.Sprintf("Get user profile error: %s - %s", e.Code, e.Message)
}

func (r *repository) UpdateProfile(userID string, updates bson.M) (*model.User, error) {
// Start a session for transaction
session, err := r.db.Client().StartSession()
Expand Down Expand Up @@ -81,3 +90,36 @@ func (r *repository) UpdateProfile(userID string, updates bson.M) (*model.User,

return &updatedUser, nil
}

func (r *repository) GetProfile(userID string) (*model.User, error) {
session, err := r.db.Client().StartSession()
if err != nil {
return nil, err
}
defer session.EndSession(context.Background())

// Convert userID to primitive.ObjectID
userIDPrimitive, err := primitive.ObjectIDFromHex(userID)
if err != nil {
return nil, &UserProfileUpdateError{util.NewBaseError("INVALID_USER_ID", "Invalid user ID format")}
}
var dataUser model.User
// Transaction handling
transactionErr := mongo.WithSession(context.Background(), session, func(sc mongo.SessionContext) error {

err = r.db.Collection("users").FindOne(sc, bson.M{"_id": userIDPrimitive}).Decode(&dataUser)
if err != nil {
if err == mongo.ErrNoDocuments {
return &GetUserProfileError{util.NewBaseError("USER_NOT_FOUND", "User not found")}
}
}

return nil
})

if transactionErr != nil {
return nil, transactionErr
}

return &dataUser, nil
}
17 changes: 17 additions & 0 deletions controllers/user/profile/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package profile

import model "github.com/donaderoyan/talentgrowth-be/models"

type Service interface {
UpdateProfileService(userID string, input *UpdateProfileInput) (*model.User, error)
PutProfileService(userID string, input *UpdateProfileInput) (*model.User, error)
GetProfileService(userID string) (*model.User, error)
}

type service struct {
repository Repository
}

func NewProfileService(repository Repository) *service {
return &service{repository: repository}
}
44 changes: 44 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,50 @@ const docTemplate = `{
}
},
"/api/v1/user/profile/{id}": {
"get": {
"description": "Get the profile of a user by their ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"User"
],
"summary": "Get user profile",
"parameters": [
{
"type": "string",
"description": "User ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/profile.UpdateProfileInput"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"500": {
"description": "Internal server error",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
},
"put": {
"description": "Update the profile of a user by their ID",
"consumes": [
Expand Down
44 changes: 44 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,50 @@
}
},
"/api/v1/user/profile/{id}": {
"get": {
"description": "Get the profile of a user by their ID",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"User"
],
"summary": "Get user profile",
"parameters": [
{
"type": "string",
"description": "User ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/profile.UpdateProfileInput"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"500": {
"description": "Internal server error",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
},
"put": {
"description": "Update the profile of a user by their ID",
"consumes": [
Expand Down
30 changes: 30 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,36 @@ paths:
tags:
- User
/api/v1/user/profile/{id}:
get:
consumes:
- application/json
description: Get the profile of a user by their ID
parameters:
- description: User ID
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/profile.UpdateProfileInput'
"400":
description: Bad request
schema:
additionalProperties: true
type: object
"500":
description: Internal server error
schema:
additionalProperties: true
type: object
summary: Get user profile
tags:
- User
patch:
consumes:
- application/json
Expand Down
55 changes: 55 additions & 0 deletions handlers/user/profile/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package profilehandler

import (
"net/http"

profile "github.com/donaderoyan/talentgrowth-be/controllers/user/profile"
util "github.com/donaderoyan/talentgrowth-be/utils"
"github.com/gin-gonic/gin"
)

// Swagger documentation for GetProfileHandler
// @Summary Get user profile
// @Description Get the profile of a user by their ID
// @Tags User
// @Accept json
// @Produce json
// @Param id path string true "User ID"
// @Success 200 {object} profile.UpdateProfileInput
// @Failure 400 {object} map[string]interface{} "Bad request"
// @Failure 500 {object} map[string]interface{} "Internal server error"
// @Router /api/v1/user/profile/{id} [get]
func (h *handler) GetProfileHandler(ctx *gin.Context) {
userID := ctx.Param("id")
if userID == "" {
util.ErrorResponse(ctx, "Update profile failed", http.StatusBadRequest, http.MethodPut, "ID is required")
return
}

dataUser, errData := h.service.GetProfileService(userID)
if errData != nil {
switch errData.(type) {
case *profile.GetUserProfileError:
util.ErrorResponse(ctx, "Get user profile failed", http.StatusBadRequest, http.MethodPatch, errData.Error())
return
default:
// Handle other unexpected errors
util.ErrorResponse(ctx, "Internal server error", http.StatusInternalServerError, http.MethodPatch, nil)
return
}
}

// masking data
responseData := gin.H{
"firstName": dataUser.FirstName,
"lastName": dataUser.LastName,
"phone": dataUser.Phone,
"address": dataUser.Address,
"birthday": dataUser.Birthday,
"gender": dataUser.Gender,
"nationality": dataUser.Nationality,
"bio": dataUser.Bio,
}

util.APIResponse(ctx, "Get user profile successfully", http.StatusOK, http.MethodPatch, responseData)
}
11 changes: 11 additions & 0 deletions handlers/user/profile/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package profilehandler

import profile "github.com/donaderoyan/talentgrowth-be/controllers/user/profile"

type handler struct {
service profile.Service
}

func NewHandlerProfile(service profile.Service) *handler {
return &handler{service: service}
}
18 changes: 7 additions & 11 deletions handlers/user/profile/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import (
util "github.com/donaderoyan/talentgrowth-be/utils"
)

type handler struct {
service profile.Service
}

func NewHandlerProfile(service profile.Service) *handler {
return &handler{service: service}
}

// Swagger documentation for UpdateProfileHandler
// @Summary Update user profile (partial update)
// @Description Update the profile of a user by their ID. Only the fields that are provided will be updated.
Expand All @@ -30,7 +22,11 @@ func NewHandlerProfile(service profile.Service) *handler {
// @Failure 500 {object} map[string]interface{} "Internal server error"
// @Router /api/v1/user/profile/{id} [patch]
func (h *handler) UpdateProfileHandler(ctx *gin.Context) {
userID := ctx.Param("id") // Assuming 'id' is passed as a URL parameter
userID := ctx.Param("id")
if userID == "" {
util.ErrorResponse(ctx, "Update profile failed", http.StatusBadRequest, http.MethodPut, "ID is required")
return
}
var input profile.UpdateProfileInput

if err := ctx.ShouldBindJSON(&input); err != nil {
Expand All @@ -57,8 +53,8 @@ func (h *handler) UpdateProfileHandler(ctx *gin.Context) {
}

responseData := gin.H{
"first_name": updatedUser.FirstName,
"last_name": updatedUser.LastName,
"firstName": updatedUser.FirstName,
"lastName": updatedUser.LastName,
"phone": updatedUser.Phone,
"address": updatedUser.Address,
"birthday": updatedUser.Birthday,
Expand Down
Loading

0 comments on commit 7119973

Please sign in to comment.