Skip to content

Commit

Permalink
fix: issue controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
sreehari2003 committed Sep 17, 2023
1 parent 1fbe232 commit d77a32c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
38 changes: 23 additions & 15 deletions controller/issue.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controller

import (
"encoding/json"
"fmt"
"io"
"net/http"

Expand Down Expand Up @@ -62,14 +61,24 @@ func (h Handler) CreateIssue(c *gin.Context) {
return
}
// creating data in db
h.DB.Create(&issue)
res, err := h.DB.NewInsert().Model(&issue).Exec(c)

if err != nil {
// sending succes message with data to client
c.JSON(http.StatusCreated, gin.H{
"status": http.StatusExpectationFailed,
"response": "Issue creation failed",
"ok": false,
})
return
}

// sending succes message with data to client
c.JSON(http.StatusCreated, gin.H{
"status": http.StatusCreated,
"response": "Issue created successfully",
"ok": true,
"data": issue,
"data": res,
})

}
Expand All @@ -82,13 +91,16 @@ func (h Handler) GetAllIssues(c *gin.Context) {
// results will be stored in this variable
// if request is successful
var issues []models.Issue
if result := h.DB.Find(&issues); result.Error != nil {
err := h.DB.NewSelect().Model(&issues).Scan(c)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"error": "couldn't find the issues",
"ok": false,
})
return
}

c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"response": "Issues read successfully",
Expand All @@ -104,7 +116,7 @@ func (h Handler) GetAllIssues(c *gin.Context) {
func (h Handler) GetIssueByID(c *gin.Context) {
var issues []models.Issue
ID := c.Param("id")
if result := h.DB.Find(&issues, ID).Preload("form"); result.Error != nil {
if result := h.DB.NewSelect().Model(&issues).Where("id=?", ID).Scan(c); result.Error != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"error": "couldn't find the issue",
Expand Down Expand Up @@ -138,7 +150,7 @@ func (h Handler) SearchIssueByPostNumber(c *gin.Context) {
return
}

if result := h.DB.Where("post_id = ?", post_id).Find(&issues); result.Error != nil {
if result := h.DB.NewSelect().Model(&issues).Where("post_id = ?", post_id).Scan(c); result.Error != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"error": "couldn't find the issues",
Expand All @@ -158,7 +170,7 @@ func (h Handler) SearchIssueByPostNumber(c *gin.Context) {
func (h Handler) GetIssueWithFormHandler(c *gin.Context) {
ID := c.Param("id")
var issue models.Issue
if result := h.DB.Preload("Form").First(&issue, ID); result.Error != nil {
if result := h.DB.NewSelect().Model(&issue).Where("id=?", ID).Scan(c); result.Error != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"error": "couldn't find the issue",
Expand All @@ -176,24 +188,20 @@ func (h Handler) GetIssueWithFormHandler(c *gin.Context) {

func (h Handler) DeleteAllIssue(c *gin.Context) {
var issues []models.Issue
if err := h.DB.Preload("Form").Find(&issues).Error; err != nil {
res, err := h.DB.NewDelete().Model(&issues).Exec(c)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"error": "couldn't Delete the issue",
"ok": false,
})
return
}

for _, issue := range issues {
h.DB.Delete(&issue)
}

fmt.Print("api is here nice")

c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"response": "All Issue Was Deleted successfully",
"ok": true,
"data": nil,
"data": res,
})
}
2 changes: 1 addition & 1 deletion controller/officials.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (h Handler) CreateOffical(c *gin.Context) {
})
return
}

h.DB.NewInsert()
err = json.Unmarshal(body, &officials)
if err != nil {
errList["Invalid_body"] = "Invalid data provided"
Expand Down
11 changes: 6 additions & 5 deletions models/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package models
import (
"errors"

"gorm.io/gorm"
"github.com/uptrace/bun"
)

// swagger:model Issue
type Issue struct {
gorm.Model
Title string `gorm:"size:255;not null" json:"title"`
Desc string `gorm:"size:255;not null" json:"desc"`
PostID string `gorm:"not null" json:"post_id"`
bun.BaseModel `bun:"table:issue,alias:u"`
ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title,notnull"`
Desc string `bun:"desc,notnull"`
PostID string `bun:"post_id,notnull"`
}

// custom vaidation for body data from backend
Expand Down

0 comments on commit d77a32c

Please sign in to comment.