|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gin-gonic/gin" |
| 5 | + "github.com/swimresults/user-service/dto" |
| 6 | + "github.com/swimresults/user-service/model" |
| 7 | + "github.com/swimresults/user-service/service" |
| 8 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +func notificationUserController() { |
| 13 | + router.GET("/notification_users", getNotificationUsers) |
| 14 | + router.GET("/notification_user", getNotificationUser) |
| 15 | + router.GET("/notification_user/:id", getNotificationUserById) |
| 16 | + |
| 17 | + router.POST("/notification_user", addNotificationUser) |
| 18 | + router.POST("/notification_user/register", registerNotificationUser) |
| 19 | + |
| 20 | + router.DELETE("/notification_user/:id", removeNotificationUser) |
| 21 | + |
| 22 | + router.PUT("/notification_user", updateNotificationUser) |
| 23 | + |
| 24 | + router.OPTIONS("/notification_user", okay) |
| 25 | + router.OPTIONS("/notification_user/register", okay) |
| 26 | +} |
| 27 | + |
| 28 | +func getNotificationUsers(c *gin.Context) { |
| 29 | + |
| 30 | + if failIfNotRoot(c) { |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + users, err := service.GetNotificationUsers() |
| 35 | + if err != nil { |
| 36 | + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + c.IndentedJSON(http.StatusOK, users) |
| 41 | +} |
| 42 | + |
| 43 | +func getNotificationUser(c *gin.Context) { |
| 44 | + |
| 45 | + claims, err1 := getClaimsFromAuthHeader(c) |
| 46 | + |
| 47 | + if err1 != nil { |
| 48 | + c.IndentedJSON(http.StatusNotFound, gin.H{"message": err1.Error()}) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + user, err := service.GetUserByKeycloakId(claims.Sub) |
| 53 | + if err != nil { |
| 54 | + c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()}) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + notificationUser, err2 := service.GetNotificationUserById(user.Identifier) |
| 59 | + if err2 != nil { |
| 60 | + c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()}) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + c.IndentedJSON(http.StatusOK, notificationUser) |
| 65 | +} |
| 66 | + |
| 67 | +func getNotificationUserById(c *gin.Context) { |
| 68 | + |
| 69 | + if failIfNotRoot(c) { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + id, convErr := primitive.ObjectIDFromHex(c.Param("id")) |
| 74 | + if convErr != nil { |
| 75 | + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given id was not of type ObjectID"}) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + user, err := service.GetNotificationUserById(id) |
| 80 | + if err != nil { |
| 81 | + c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()}) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + c.IndentedJSON(http.StatusOK, user) |
| 86 | +} |
| 87 | + |
| 88 | +func removeNotificationUser(c *gin.Context) { |
| 89 | + |
| 90 | + if failIfNotRoot(c) { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + id, convErr := primitive.ObjectIDFromHex(c.Param("id")) |
| 95 | + if convErr != nil { |
| 96 | + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given id was not of type ObjectID"}) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + err := service.RemoveNotificationUserById(id) |
| 101 | + if err != nil { |
| 102 | + c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()}) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + c.IndentedJSON(http.StatusNoContent, "") |
| 107 | +} |
| 108 | + |
| 109 | +func addNotificationUser(c *gin.Context) { |
| 110 | + |
| 111 | + if failIfNotRoot(c) { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + var user model.NotificationUser |
| 116 | + if err := c.BindJSON(&user); err != nil { |
| 117 | + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + r, err := service.AddNotificationUser(user) |
| 122 | + if err != nil { |
| 123 | + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + c.IndentedJSON(http.StatusOK, r) |
| 128 | +} |
| 129 | + |
| 130 | +func updateNotificationUser(c *gin.Context) { |
| 131 | + |
| 132 | + if failIfNotRoot(c) { |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + var user model.NotificationUser |
| 137 | + if err := c.BindJSON(&user); err != nil { |
| 138 | + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + r, err := service.UpdateNotificationUser(user) |
| 143 | + if err != nil { |
| 144 | + c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()}) |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + c.IndentedJSON(http.StatusOK, r) |
| 149 | +} |
| 150 | + |
| 151 | +func registerNotificationUser(c *gin.Context) { |
| 152 | + var request dto.RegisterNotificationUserRequestDto |
| 153 | + if err := c.BindJSON(&request); err != nil { |
| 154 | + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + r, err := service.RegisterNotificationUser(request.Token) |
| 159 | + if err != nil { |
| 160 | + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + c.IndentedJSON(http.StatusOK, r) |
| 165 | +} |
0 commit comments