forked from red-gold/telar-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
65 lines (53 loc) · 2.35 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package handlers
import (
"fmt"
"net/http"
"github.com/GMcD/telar-web/micros/profile/database"
"github.com/GMcD/telar-web/micros/profile/dto"
service "github.com/GMcD/telar-web/micros/profile/services"
"github.com/gofiber/fiber/v2"
"github.com/red-gold/telar-core/pkg/log"
utils "github.com/red-gold/telar-core/utils"
)
// InitProfileIndexHandle handle create a new index
func InitProfileIndexHandle(c *fiber.Ctx) error {
// Create service
profileService, serviceErr := service.NewUserProfileService(database.Db)
if serviceErr != nil {
errorMessage := fmt.Sprintf("Profile service Error %s", serviceErr.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/userProfileService", "Error happened while creating userProfileService!"))
}
postIndexMap := make(map[string]interface{})
postIndexMap["fullName"] = "text"
postIndexMap["objectId"] = 1
if err := profileService.CreateUserProfileIndex(postIndexMap); err != nil {
errorMessage := fmt.Sprintf("Create post index Error %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("createPostIndex", "Error happened while creating post index!"))
}
return c.SendStatus(http.StatusOK)
}
// CreateProfileHandle handle create a new profile
func CreateDtoProfileHandle(c *fiber.Ctx) error {
// Create service
profileService, serviceErr := service.NewUserProfileService(database.Db)
if serviceErr != nil {
errorMessage := fmt.Sprintf("Profile service Error %s", serviceErr.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("internal/userProfileService", "Error happened while creating userProfileService!"))
}
model := new(dto.UserPublicProfile)
err := c.BodyParser(model)
if err != nil {
errorMessage := fmt.Sprintf("parse user profile model %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("parseUserProfileModel", "Error happened while parsing model!"))
}
if err = profileService.SaveUserProfile(model); err != nil {
errorMessage := fmt.Sprintf("Create profile error %s", err.Error())
log.Error(errorMessage)
return c.Status(http.StatusInternalServerError).JSON(utils.Error("createProfileError", "Error happened while saving user profile!"))
}
return c.SendStatus(http.StatusOK)
}