From e0b0c7edb6a55eaae1628b0363ea5c2ec8dbab54 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 12 Jul 2021 20:32:22 +0200 Subject: [PATCH] Added func for delete user --- routers/api/v1/user/user.go | 156 ++++++++++++++++++++++++++++++------ 1 file changed, 133 insertions(+), 23 deletions(-) diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index 1b77faf..6d1ce31 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -2,6 +2,8 @@ package user import ( "crypto/rand" + "encoding/json" + "errors" "math/big" "regexp" "strings" @@ -9,6 +11,7 @@ import ( "git.umbach.dev/app-idea/rest-api/modules/config" "git.umbach.dev/app-idea/rest-api/modules/database" + "git.umbach.dev/app-idea/rest-api/modules/rabbitmq" "git.umbach.dev/app-idea/rest-api/modules/structs" "github.com/gofiber/fiber/v2" log "github.com/sirupsen/logrus" @@ -133,40 +136,146 @@ func getUserIdBySessionId(sessionId string) (string, error) { return session.UserId, nil } +func GetUserById(c *fiber.Ctx) error { + // swagger:operation GET /users User user + // --- + // summary: Informations about the user by id (except password) + // parameters: + // - name: v + // in: query + // description: Example -> { "v"; ["name", "state", "language_id"] } + // type: string + // required: true + // responses: + // '200': + // description: User informations + // '400': + // description: Values wrong format + + return userInfos(c, c.Params("id")) +} + func GetUser(c *fiber.Ctx) error { - log.Infoln("body", c.Body()) + // swagger:operation GET /user User user + // --- + // summary: Informations about the user (except password) + // parameters: + // - name: v + // in: query + // description: Example -> { "v"; ["name", "state", "language_id"] } + // type: string + // required: true + // responses: + // '200': + // description: User informations + // '400': + // description: Values wrong format + return userInfos(c, "") +} + +func userInfos(c *fiber.Ctx, userId string) error { type Input struct { - Val []string `json:"val"` + Values []string `json:"v"` } - log.Infoln("arr", string(c.Body())) - var input Input if err := c.BodyParser(&input); err != nil { - log.Infoln("bad", input) return c.SendStatus(fiber.StatusBadRequest) } - log.Infoln("test", strings.Join(input.Val, ",")) + value := strings.Join(input.Values, ",") - log.Println("input", input) + if strings.Contains(value, "password") { + return c.SendStatus(fiber.StatusForbidden) + } db := database.DB - userId, err := getUserIdBySessionId(c.Cookies("session_id")) + + if userId == "" { + var err error + + userId, err = getUserIdBySessionId(c.Cookies("session_id")) + + if err != nil { + return c.SendStatus(fiber.StatusInternalServerError) + } + } + + user := map[string]interface{}{} + + res := db.Model(&structs.User{}).Select(value).First(&user, "id = ?", userId) + + if res.Error != nil { + return c.SendStatus(fiber.StatusBadRequest) + } + + return c.JSON(&user) +} + +func getUserBySessionId(sessionId string) (structs.User, error) { + db := database.DB + user := structs.User{} + userId, err := getUserIdBySessionId(sessionId) + + if err != nil { + return user, err + } + + err = db.Where("id = ?", userId).First(&user).Error + + if errors.Is(err, gorm.ErrRecordNotFound) { + return user, err + } + + log.Infoln("getUserBySessionId", user) + + return user, nil +} + +func deleteUser(c *fiber.Ctx) error { + userAction := structs.UserAction{Id: c.Params("actionId")} + + db := database.DB + + db.First(&userAction) + + db.Delete(&structs.User{Id: userAction.UserId}) + db.Where("user_id = ?", userAction.UserId).Delete(&structs.Session{}) + db.Delete(&userAction) + + return c.SendStatus(fiber.StatusOK) +} + +func DeleteUser(c *fiber.Ctx) error { + user, err := getUserBySessionId(c.Cookies("session_id")) if err != nil { return c.SendStatus(fiber.StatusInternalServerError) } - user := structs.User{} + userActivationId, err := createUserAction(user.Id) - db.Table("users").Select(strings.Join(input.Val, ",")).First(&user, "id = ?", userId) + if err != nil { + return c.SendStatus(fiber.StatusInternalServerError) + } - return c.JSON(&user) + user.State = 2 + + db := database.DB + + db.Save(&user) + + rabbitmq.PublishMail(user.Email, 1, user.LanguageId, json.RawMessage(`{"name": "`+user.Name+`", + + "email": "`+user.Email+`", + "url": "http://localhost:3000/api/v1/user/action/1/`+userActivationId+`"}`)) + + return c.SendStatus(fiber.StatusOK) } +/* func GetUsers(c *fiber.Ctx) error { list := []string{} @@ -174,17 +283,18 @@ func GetUsers(c *fiber.Ctx) error { var ( name string )*/ - /* - rows, err := db.Query("SELECT username FROM users;") - fmt.Println("err", err) - defer rows.Close() - fmt.Println("reading data:") - for rows.Next() { - err := rows.Scan(&name) - fmt.Printf("Data row = (%s, %s)\n", name, err) - list = append(list, name) - } - err = rows.Err()*/ - +/* + rows, err := db.Query("SELECT username FROM users;") + fmt.Println("err", err) + defer rows.Close() + fmt.Println("reading data:") + for rows.Next() { + err := rows.Scan(&name) + fmt.Printf("Data row = (%s, %s)\n", name, err) + list = append(list, name) + } + err = rows.Err()*/ +/* return c.JSON(list) } +*/