From 1f6f863081a3afa8e321339e78b5804a4518b2bf Mon Sep 17 00:00:00 2001 From: Sep Date: Thu, 10 Apr 2025 15:43:16 +0330 Subject: [PATCH 1/2] report comments and answers --- database/db.go | 2 +- handlers/report_handler.go | 45 ++++++++++++++++++++++++++++++++++++++ models/report.go | 7 ++++++ routers/routes.go | 3 +++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 handlers/report_handler.go create mode 100644 models/report.go diff --git a/database/db.go b/database/db.go index dfc8d84..4d66839 100644 --- a/database/db.go +++ b/database/db.go @@ -30,7 +30,7 @@ func ConnectDatabase() { log.Fatalf("Failed to connect to database: %v", err) } - err = DB.AutoMigrate(&models.User{}, &models.Question{}, &models.Answer{}, &models.Comment{}, &models.Tag{}, &models.Log{}) + err = DB.AutoMigrate(&models.User{}, &models.Question{}, &models.Answer{}, &models.Comment{}, &models.Tag{}, &models.Log{}, &models.Report{}) if err != nil { log.Fatalf("Migration failed: %v", err) } diff --git a/handlers/report_handler.go b/handlers/report_handler.go new file mode 100644 index 0000000..76f40cc --- /dev/null +++ b/handlers/report_handler.go @@ -0,0 +1,45 @@ +package handlers + +import ( + "Learning/database" + "Learning/models" + "github.com/gin-gonic/gin" + "net/http" +) + +// ReportInteraction +// @Tags report +// @Accept json +// @Produce json +// @Param Authorization header string true "Bearer Token" +// @Param report body models.Report true "Report object" +// @Success 201 {object} models.Report +// @Router /api/report [post] +func ReportInteraction(c *gin.Context) { + var report models.Report + if err := c.ShouldBindJSON(&report); err != nil { + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "invalid json format"}) + return + } + if report.ReportType == "comment" { + var comment models.Comment + if commentQueryResult := database.DB.First(&comment, report.ReportId); commentQueryResult.Error != nil { + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "comment not found"}) + return + } + } else if report.ReportType == "answer" { + var answer models.Answer + if commentQueryResult := database.DB.First(&answer, report.ReportId); commentQueryResult.Error != nil { + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "answer not found"}) + return + } + } else { + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "invalid parent type"}) + return + } + if reportCreationResult := database.DB.Create(&report); reportCreationResult.Error != nil { + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "failed to create report"}) + return + } + c.IndentedJSON(http.StatusCreated, &report) +} diff --git a/models/report.go b/models/report.go new file mode 100644 index 0000000..3936b50 --- /dev/null +++ b/models/report.go @@ -0,0 +1,7 @@ +package models + +type Report struct { + ReportId string `json:"report_id"` + ReportType string `json:"report_type"` + Description string `json:"description"` +} diff --git a/routers/routes.go b/routers/routes.go index 2834628..7a49eb3 100644 --- a/routers/routes.go +++ b/routers/routes.go @@ -42,6 +42,9 @@ func SetupRouter() *gin.Engine { //tag routes protected.POST("/tag/add", handlers.AddTag) protected.POST("/tag/questions/all", handlers.FetchTagQuestions) + + //report routes + protected.POST("/report", handlers.ReportInteraction) } adminRoutes := router.Group("/admin") From b890f5cd068a0ae0da93832de692d0d44a4f9dd3 Mon Sep 17 00:00:00 2001 From: Sep Date: Thu, 10 Apr 2025 15:49:28 +0330 Subject: [PATCH 2/2] swagger updated --- docs/docs.go | 118 ++++++++++++++++++++++++++++++++++++++++------ docs/swagger.json | 98 ++++++++++++++++++++++++++++++++++++-- docs/swagger.yaml | 76 +++++++++++++++++++++++++---- 3 files changed, 265 insertions(+), 27 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 879c489..b268da9 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -79,7 +79,7 @@ const docTemplate = `{ } } }, - "/api/answer_handler/add": { + "/api/answer/add": { "post": { "consumes": [ "application/json" @@ -88,7 +88,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "answer_handler" + "answer" ], "parameters": [ { @@ -100,7 +100,7 @@ const docTemplate = `{ }, { "description": "Answer object", - "name": "answer_handler", + "name": "answer", "in": "body", "required": true, "schema": { @@ -118,7 +118,7 @@ const docTemplate = `{ } } }, - "/api/answer_handler/correctAnswer/{id}": { + "/api/answer/correctAnswer/{id}": { "get": { "consumes": [ "application/json" @@ -127,7 +127,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "answer_handler" + "answer" ], "parameters": [ { @@ -148,7 +148,7 @@ const docTemplate = `{ } } }, - "/api/answer_handler/delete": { + "/api/answer/delete": { "delete": { "consumes": [ "application/json" @@ -157,7 +157,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "answer_handler" + "answer" ], "parameters": [ { @@ -178,7 +178,7 @@ const docTemplate = `{ } } }, - "/api/answer_handler/voteDown/{id}": { + "/api/answer/voteDown/{id}": { "get": { "consumes": [ "application/json" @@ -187,7 +187,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "answer_handler" + "answer" ], "parameters": [ { @@ -215,7 +215,7 @@ const docTemplate = `{ } } }, - "/api/answer_handler/voteUp/{id}": { + "/api/answer/voteUp/{id}": { "get": { "consumes": [ "application/json" @@ -224,7 +224,7 @@ const docTemplate = `{ "application/json" ], "tags": [ - "answer_handler" + "answer" ], "parameters": [ { @@ -351,7 +351,7 @@ const docTemplate = `{ }, { "description": "Question Data", - "name": "question_handler", + "name": "question", "in": "body", "required": true, "schema": { @@ -417,7 +417,7 @@ const docTemplate = `{ }, { "description": "Question object", - "name": "question_handler", + "name": "question", "in": "body", "required": true, "schema": { @@ -465,7 +465,7 @@ const docTemplate = `{ } } }, - "/api/questions/my/{id}": { + "/api/questions/voteDown/{id}": { "get": { "consumes": [ "application/json" @@ -539,6 +539,82 @@ const docTemplate = `{ } } }, + "/api/questions/{id}": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "questions" + ], + "parameters": [ + { + "type": "string", + "description": "Bearer Token", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "string", + "description": "id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "201": { + "description": "Created", + "schema": { + "$ref": "#/definitions/models.Question" + } + } + } + } + }, + "/api/report": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "report" + ], + "parameters": [ + { + "type": "string", + "description": "Bearer Token", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "Report object", + "name": "report", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.Report" + } + } + ], + "responses": { + "201": { + "description": "Created", + "schema": { + "$ref": "#/definitions/models.Report" + } + } + } + } + }, "/api/tag/add": { "post": { "consumes": [ @@ -785,6 +861,20 @@ const docTemplate = `{ } } }, + "models.Report": { + "type": "object", + "properties": { + "description": { + "type": "string" + }, + "report_id": { + "type": "string" + }, + "report_type": { + "type": "string" + } + } + }, "models.Tag": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 3b211f7..d9e86a2 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -454,7 +454,7 @@ } } }, - "/api/questions/my/{id}": { + "/api/questions/voteDown/{id}": { "get": { "consumes": [ "application/json" @@ -528,6 +528,82 @@ } } }, + "/api/questions/{id}": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "questions" + ], + "parameters": [ + { + "type": "string", + "description": "Bearer Token", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "string", + "description": "id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "201": { + "description": "Created", + "schema": { + "$ref": "#/definitions/models.Question" + } + } + } + } + }, + "/api/report": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "report" + ], + "parameters": [ + { + "type": "string", + "description": "Bearer Token", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "Report object", + "name": "report", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/models.Report" + } + } + ], + "responses": { + "201": { + "description": "Created", + "schema": { + "$ref": "#/definitions/models.Report" + } + } + } + } + }, "/api/tag/add": { "post": { "consumes": [ @@ -611,7 +687,7 @@ "parameters": [ { "description": "User object", - "name": "answer", + "name": "answer_handler", "in": "body", "required": true, "schema": { @@ -673,7 +749,7 @@ "parameters": [ { "description": "User object", - "name": "answer", + "name": "answer_handler", "in": "body", "required": true, "schema": { @@ -736,7 +812,7 @@ "type": "integer" }, "parent_type": { - "description": "\"question\" or \"answer\"", + "description": "\"question_handler\" or \"answer_handler\"", "type": "string" }, "user_id": { @@ -774,6 +850,20 @@ } } }, + "models.Report": { + "type": "object", + "properties": { + "description": { + "type": "string" + }, + "report_id": { + "type": "string" + }, + "report_type": { + "type": "string" + } + } + }, "models.Tag": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 0a60432..0873d7d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -53,6 +53,15 @@ definitions: votes: type: integer type: object + models.Report: + properties: + description: + type: string + report_id: + type: string + report_type: + type: string + type: object models.Tag: properties: name: @@ -139,7 +148,7 @@ paths: type: string - description: Answer object in: body - name: answer_handler + name: answer required: true schema: $ref: '#/definitions/models.Answer' @@ -151,7 +160,7 @@ paths: schema: $ref: '#/definitions/models.Answer' tags: - - answer_handler + - answer /api/answer/correctAnswer/{id}: get: consumes: @@ -170,7 +179,7 @@ paths: schema: $ref: '#/definitions/models.Answer' tags: - - answer_handler + - answer /api/answer/delete: delete: consumes: @@ -189,7 +198,7 @@ paths: schema: $ref: '#/definitions/models.Answer' tags: - - answer_handler + - answer /api/answer/voteDown/{id}: get: consumes: @@ -213,7 +222,7 @@ paths: schema: $ref: '#/definitions/models.Answer' tags: - - answer_handler + - answer /api/answer/voteUp/{id}: get: consumes: @@ -237,7 +246,7 @@ paths: schema: $ref: '#/definitions/models.Answer' tags: - - answer_handler + - answer /api/comment/add: post: consumes: @@ -288,6 +297,30 @@ paths: $ref: '#/definitions/models.Comment' tags: - comment + /api/questions/{id}: + get: + consumes: + - application/json + parameters: + - description: Bearer Token + in: header + name: Authorization + required: true + type: string + - description: id + in: path + name: id + required: true + type: string + produces: + - application/json + responses: + "201": + description: Created + schema: + $ref: '#/definitions/models.Question' + tags: + - questions /api/questions/add: post: consumes: @@ -300,7 +333,7 @@ paths: type: string - description: Question Data in: body - name: question_handler + name: question required: true schema: $ref: '#/definitions/models.Question' @@ -342,7 +375,7 @@ paths: type: string - description: Question object in: body - name: question_handler + name: question required: true schema: $ref: '#/definitions/models.Question' @@ -374,7 +407,7 @@ paths: $ref: '#/definitions/models.Question' tags: - questions - /api/questions/my/{id}: + /api/questions/voteDown/{id}: get: consumes: - application/json @@ -422,6 +455,31 @@ paths: $ref: '#/definitions/models.Question' tags: - questions + /api/report: + post: + consumes: + - application/json + parameters: + - description: Bearer Token + in: header + name: Authorization + required: true + type: string + - description: Report object + in: body + name: report + required: true + schema: + $ref: '#/definitions/models.Report' + produces: + - application/json + responses: + "201": + description: Created + schema: + $ref: '#/definitions/models.Report' + tags: + - report /api/tag/add: post: consumes: