diff --git a/modules/equipment/equipment.go b/modules/equipment/equipment.go index 956255f..f5f566a 100644 --- a/modules/equipment/equipment.go +++ b/modules/equipment/equipment.go @@ -61,7 +61,7 @@ func getImageType(data string) string { } } -func CreateEquipmentDocumentation(c *fiber.Ctx, body structs.ApiCreateEquipmentDocumentationRequest) error { +func CreateEquipmentDocumentation(c *fiber.Ctx, body structs.CreateEquipmentDocumentationRequest) error { var bodyNotes []map[string]string err := json.Unmarshal(body.Notes, &bodyNotes) @@ -154,7 +154,7 @@ func GetEquipmentDocumentations(stockItemId string, c *fiber.Ctx) error { } } - return c.JSON(structs.ApiEquipmentDocumentationResponse{ + return c.JSON(structs.EquipmentDocumentationResponse{ Status: statusCode, Documentations: documentations}) } @@ -167,7 +167,7 @@ func GetEquipmentDocumentation(stockItemId string, documentationId string, c *fi return c.JSON(documentation) } -func EditEquipmentDocumentation(c *fiber.Ctx, body structs.ApiEditEquipmentDocumentationRequest) error { +func EditEquipmentDocumentation(c *fiber.Ctx, body structs.EditEquipmentDocumentationRequest) error { var bodyNotes []map[string]string err := json.Unmarshal(body.Notes, &bodyNotes) diff --git a/modules/structs/equipment.go b/modules/structs/equipment.go index ab2549a..8ddc5d1 100644 --- a/modules/structs/equipment.go +++ b/modules/structs/equipment.go @@ -5,15 +5,6 @@ import ( "time" ) -/* -type Equipment struct { - Id string // stock item id of invex system - Name string - Thumbnail string // url provided by invex system - CreatedAt time.Time - UpdatedAt time.Time -} */ - // swagger:model EquipmentDocumentation type EquipmentDocumentation struct { Id string @@ -26,31 +17,31 @@ type EquipmentDocumentation struct { UpdatedAt time.Time } -type ApiEquipmentRequest struct { +type EquipmentRequest struct { StockItemId string `json:"stockItemId"` } -type ApiGetDocumentationEquipmentRequest struct { +type GetDocumentationEquipmentRequest struct { StockItemId string `json:"stockItemId"` DocumentationId string `json:"documentationId"` } -// swagger:model ApiCreateEquipmentDocumentationRequest -type ApiCreateEquipmentDocumentationRequest struct { +// swagger:model CreateEquipmentDocumentationRequest +type CreateEquipmentDocumentationRequest struct { StockItemId string `json:"stockItemId"` Type uint8 `json:"type"` Title string `json:"title"` Notes json.RawMessage `json:"notes"` } -// swagger:model ApiEquipmentDocumentationResponse -type ApiEquipmentDocumentationResponse struct { +// swagger:model EquipmentDocumentationResponse +type EquipmentDocumentationResponse struct { Status int Documentations []EquipmentDocumentation } -// swagger:model ApiEditEquipmentDocumentationRequest -type ApiEditEquipmentDocumentationRequest struct { +// swagger:model EditEquipmentDocumentationRequest +type EditEquipmentDocumentationRequest struct { DocumentationId string `json:"documentationId"` Type uint8 `json:"type"` Title string `json:"title"` diff --git a/modules/structs/user.go b/modules/structs/user.go index b1f208e..3b3e0ba 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -26,11 +26,13 @@ type UserSession struct { ExpiresAt time.Time } +// swagger:model UserLoginRequest type UserLoginRequest struct { Username string Password string } +// swagger:model UserLoginResponse type UserLoginResponse struct { Session string } diff --git a/modules/utils/validator.go b/modules/utils/validator.go index bcb30e7..491c688 100644 --- a/modules/utils/validator.go +++ b/modules/utils/validator.go @@ -48,8 +48,8 @@ func ValidatorInit() { structs.ApiGroupTaskRequest{}) Validate.RegisterStructValidationMapRules(equipmentRules, - structs.ApiEquipmentRequest{}, - structs.ApiGetDocumentationEquipmentRequest{}, - structs.ApiCreateEquipmentDocumentationRequest{}, - structs.ApiEditEquipmentDocumentationRequest{}) + structs.EquipmentRequest{}, + structs.GetDocumentationEquipmentRequest{}, + structs.CreateEquipmentDocumentationRequest{}, + structs.EditEquipmentDocumentationRequest{}) } diff --git a/public/swagger/swagger.json b/public/swagger/swagger.json index 12a7cb8..65281b7 100644 --- a/public/swagger/swagger.json +++ b/public/swagger/swagger.json @@ -39,7 +39,7 @@ "name": "body", "in": "body", "schema": { - "$ref": "#/definitions/ApiCreateEquipmentDocumentationRequest" + "$ref": "#/definitions/CreateEquipmentDocumentationRequest" } } ], @@ -82,7 +82,7 @@ "name": "body", "in": "body", "schema": { - "$ref": "#/definitions/ApiEditEquipmentDocumentationRequest" + "$ref": "#/definitions/EditEquipmentDocumentationRequest" } } ], @@ -186,7 +186,7 @@ "200": { "description": "Equipment documentations. Status 200 if equipment documentations found, status 404 if stock item not found on invex, status 401 if backend has no permissions to access invex", "schema": { - "$ref": "#/definitions/ApiEquipmentDocumentationResponse" + "$ref": "#/definitions/EquipmentDocumentationResponse" } }, "400": { @@ -284,10 +284,188 @@ } } } + }, + "/user/auth/login": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Login user", + "operationId": "userLogin", + "parameters": [ + { + "name": "body", + "in": "body", + "schema": { + "$ref": "#/definitions/UserLoginRequest" + } + } + ], + "responses": { + "200": { + "description": "User logged in successfully", + "schema": { + "$ref": "#/definitions/UserLoginResponse" + } + }, + "400": { + "description": "Invalid request body" + }, + "401": { + "description": "Incorrect password or user deactivated" + }, + "500": { + "description": "Failed to login user" + } + } + } + }, + "/user/auth/logout": { + "delete": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Logout user", + "operationId": "userLogout", + "parameters": [ + { + "description": "User session id", + "name": "X-Authorization", + "in": "header" + } + ], + "responses": { + "201": { + "description": "User logged out successfully" + }, + "500": { + "description": "Failed to logout user" + } + } + } + }, + "/user/avatar": { + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Update user avatar", + "operationId": "userAvatar", + "parameters": [ + { + "description": "You can create a new api key in your user profile", + "name": "X-Api-Key", + "in": "header" + }, + { + "type": "file", + "description": "Avatar file", + "name": "file", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "User avatar updated successfully" + }, + "400": { + "description": "Invalid request body" + }, + "401": { + "description": "No permissions" + }, + "413": { + "description": "File too large" + }, + "422": { + "description": "Invalid file type" + }, + "500": { + "description": "Failed to update user avatar" + } + } + } + }, + "/user/session/{idForDeletion}": { + "delete": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Sign out user session", + "operationId": "userSignOutSession", + "parameters": [ + { + "description": "You can create a new api key in your user profile", + "name": "X-Api-Key", + "in": "header" + }, + { + "description": "Id for deletion", + "name": "idForDeletion", + "in": "path" + } + ], + "responses": { + "200": { + "description": "User session signed out successfully" + }, + "400": { + "description": "Invalid request body" + }, + "401": { + "description": "No permissions" + }, + "500": { + "description": "Failed to sign out user session" + } + } + } } }, "definitions": { - "ApiCreateEquipmentDocumentationRequest": { + "ApiGroupTaskRequest": { + "type": "object", + "properties": { + "Category": { + "type": "string" + }, + "Description": { + "type": "string" + }, + "GlobalInputs": { + "type": "object" + }, + "GroupId": { + "type": "string" + } + }, + "x-go-package": "jannex/admin-dashboard-backend/modules/structs" + }, + "CreateEquipmentDocumentationRequest": { "type": "object", "properties": { "notes": { @@ -310,7 +488,7 @@ }, "x-go-package": "jannex/admin-dashboard-backend/modules/structs" }, - "ApiEditEquipmentDocumentationRequest": { + "EditEquipmentDocumentationRequest": { "type": "object", "properties": { "documentationId": { @@ -333,40 +511,6 @@ }, "x-go-package": "jannex/admin-dashboard-backend/modules/structs" }, - "ApiEquipmentDocumentationResponse": { - "type": "object", - "properties": { - "Documentations": { - "type": "array", - "items": { - "$ref": "#/definitions/EquipmentDocumentation" - } - }, - "Status": { - "type": "integer", - "format": "int64" - } - }, - "x-go-package": "jannex/admin-dashboard-backend/modules/structs" - }, - "ApiGroupTaskRequest": { - "type": "object", - "properties": { - "Category": { - "type": "string" - }, - "Description": { - "type": "string" - }, - "GlobalInputs": { - "type": "object" - }, - "GroupId": { - "type": "string" - } - }, - "x-go-package": "jannex/admin-dashboard-backend/modules/structs" - }, "EquipmentDocumentation": { "type": "object", "properties": { @@ -399,6 +543,43 @@ } }, "x-go-package": "jannex/admin-dashboard-backend/modules/structs" + }, + "EquipmentDocumentationResponse": { + "type": "object", + "properties": { + "Documentations": { + "type": "array", + "items": { + "$ref": "#/definitions/EquipmentDocumentation" + } + }, + "Status": { + "type": "integer", + "format": "int64" + } + }, + "x-go-package": "jannex/admin-dashboard-backend/modules/structs" + }, + "UserLoginRequest": { + "type": "object", + "properties": { + "Password": { + "type": "string" + }, + "Username": { + "type": "string" + } + }, + "x-go-package": "jannex/admin-dashboard-backend/modules/structs" + }, + "UserLoginResponse": { + "type": "object", + "properties": { + "Session": { + "type": "string" + } + }, + "x-go-package": "jannex/admin-dashboard-backend/modules/structs" } } } \ No newline at end of file diff --git a/routers/router/api/v1/equipment/equipment.go b/routers/router/api/v1/equipment/equipment.go index 27457f5..2b55953 100644 --- a/routers/router/api/v1/equipment/equipment.go +++ b/routers/router/api/v1/equipment/equipment.go @@ -23,7 +23,7 @@ func CreateEquipmentDocumentation(c *fiber.Ctx) error { // - name: body // in: body // schema: - // "$ref": "#/definitions/ApiCreateEquipmentDocumentationRequest" + // "$ref": "#/definitions/CreateEquipmentDocumentationRequest" // responses: // '200': // description: New equipment documentation created successfully @@ -34,7 +34,7 @@ func CreateEquipmentDocumentation(c *fiber.Ctx) error { // '500': // description: Failed to create equipment documentation - var body structs.ApiCreateEquipmentDocumentationRequest + var body structs.CreateEquipmentDocumentationRequest if err := utils.BodyParserHelper(c, &body); err != nil { return c.SendStatus(fiber.StatusBadRequest) @@ -64,7 +64,7 @@ func GetEquipmentDocumentations(c *fiber.Ctx) error { // '200': // description: Equipment documentations. Status 200 if equipment documentations found, status 404 if stock item not found on invex, status 401 if backend has no permissions to access invex // schema: - // "$ref": "#/definitions/ApiEquipmentDocumentationResponse" + // "$ref": "#/definitions/EquipmentDocumentationResponse" // '400': // description: Invalid request body // '401': @@ -72,7 +72,7 @@ func GetEquipmentDocumentations(c *fiber.Ctx) error { // '500': // description: Failed to get equipment documentations - var params structs.ApiEquipmentRequest + var params structs.EquipmentRequest if err := utils.ParamsParserHelper(c, ¶ms); err != nil { return c.SendStatus(fiber.StatusBadRequest) @@ -115,7 +115,7 @@ func GetEquipmentDocumentation(c *fiber.Ctx) error { // '500': // description: Failed to get equipment documentation - var params structs.ApiGetDocumentationEquipmentRequest + var params structs.GetDocumentationEquipmentRequest if err := utils.ParamsParserHelper(c, ¶ms); err != nil { return c.SendStatus(fiber.StatusBadRequest) @@ -139,7 +139,7 @@ func EditEquipmentDocumentation(c *fiber.Ctx) error { // - name: body // in: body // schema: - // "$ref": "#/definitions/ApiEditEquipmentDocumentationRequest" + // "$ref": "#/definitions/EditEquipmentDocumentationRequest" // responses: // '200': // description: Equipment documentation edited successfully @@ -150,7 +150,7 @@ func EditEquipmentDocumentation(c *fiber.Ctx) error { // '500': // description: Failed to edit equipment documentation - var body structs.ApiEditEquipmentDocumentationRequest + var body structs.EditEquipmentDocumentationRequest if err := utils.BodyParserHelper(c, &body); err != nil { return c.SendStatus(fiber.StatusBadRequest) @@ -183,7 +183,7 @@ func GetEquipmentThumbnail(c *fiber.Ctx) error { // '500': // description: Failed to get equipment thumbnail - var params structs.ApiEquipmentRequest + var params structs.EquipmentRequest if err := utils.ParamsParserHelper(c, ¶ms); err != nil { return c.SendStatus(fiber.StatusBadRequest) diff --git a/routers/router/api/v1/jxscanner/scanner.go b/routers/router/api/v1/jnxscanner/scanner.go similarity index 100% rename from routers/router/api/v1/jxscanner/scanner.go rename to routers/router/api/v1/jnxscanner/scanner.go diff --git a/routers/router/api/v1/user/auth.go b/routers/router/api/v1/user/auth.go index 3beed4a..ef40a9d 100644 --- a/routers/router/api/v1/user/auth.go +++ b/routers/router/api/v1/user/auth.go @@ -15,15 +15,33 @@ import ( ) func UserLogin(c *fiber.Ctx) error { + // swagger:operation POST /user/auth/login user userLogin + // --- + // summary: Login user + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: body + // in: body + // schema: + // "$ref": "#/definitions/UserLoginRequest" + // responses: + // '200': + // description: User logged in successfully + // schema: + // "$ref": "#/definitions/UserLoginResponse" + // '400': + // description: Invalid request body + // '401': + // description: Incorrect password or user deactivated + // '500': + // description: Failed to login user + var body structs.UserLoginRequest - if err := c.BodyParser(&body); err != nil { - log.Error().Msg("Failed to parse body, err: " + err.Error()) - return c.Status(fiber.StatusBadRequest).JSON(err) - } - - if err := utils.ValidateStruct(body); err != nil { - log.Error().Msgf("Failed to validate body, err: %v", err) + if err := utils.BodyParserHelper(c, &body); err != nil { return c.SendStatus(fiber.StatusBadRequest) } @@ -31,7 +49,7 @@ func UserLogin(c *fiber.Ctx) error { if err != nil { log.Error().Msg("Failed to decode base64 password, err: " + err.Error()) - return c.Status(fiber.StatusBadRequest).JSON(err.Error()) + return c.SendStatus(fiber.StatusBadRequest) } if passwordValid := utils.IsPasswordLengthValid(string(decodedPassword)); !passwordValid { @@ -84,6 +102,23 @@ func UserLogin(c *fiber.Ctx) error { } func UserLogout(c *fiber.Ctx) error { + // swagger:operation DELETE /user/auth/logout user userLogout + // --- + // summary: Logout user + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: X-Authorization + // in: header + // description: User session id + // responses: + // '201': + // description: User logged out successfully + // '500': + // description: Failed to logout user + session := utils.GetXAuhorizationHeader(c) database.DB.Delete(&structs.UserSession{}, "id = ?", session) diff --git a/routers/router/api/v1/user/avatar.go b/routers/router/api/v1/user/avatar.go index 2615331..e66964f 100644 --- a/routers/router/api/v1/user/avatar.go +++ b/routers/router/api/v1/user/avatar.go @@ -17,6 +17,35 @@ import ( ) func UpdateAvatar(c *fiber.Ctx) error { + // swagger:operation POST /user/avatar user userAvatar + // --- + // summary: Update user avatar + // consumes: + // - multipart/form-data + // produces: + // - application/json + // parameters: + // - name: X-Api-Key + // in: header + // description: You can create a new api key in your user profile + // - name: file + // in: formData + // type: file + // description: Avatar file + // responses: + // '200': + // description: User avatar updated successfully + // '400': + // description: Invalid request body + // '401': + // description: No permissions + // '413': + // description: File too large + // '422': + // description: Invalid file type + // '500': + // description: Failed to update user avatar + fileHeader, err := c.FormFile("file") if err != nil { diff --git a/routers/router/api/v1/user/session.go b/routers/router/api/v1/user/session.go index be6207a..f695d02 100644 --- a/routers/router/api/v1/user/session.go +++ b/routers/router/api/v1/user/session.go @@ -12,6 +12,30 @@ import ( ) func SignOutSession(c *fiber.Ctx) error { + // swagger:operation DELETE /user/session/{idForDeletion} user userSignOutSession + // --- + // summary: Sign out user session + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: X-Api-Key + // in: header + // description: You can create a new api key in your user profile + // - name: idForDeletion + // in: path + // description: Id for deletion + // responses: + // '200': + // description: User session signed out successfully + // '400': + // description: Invalid request body + // '401': + // description: No permissions + // '500': + // description: Failed to sign out user session + var params structs.UserSignOutSessionRequest if err := c.ParamsParser(¶ms); err != nil {