|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/gin-gonic/gin" |
| 8 | + "github.com/kunalkumar-1/Evently/internals/database" |
| 9 | +) |
| 10 | + |
| 11 | +// create event |
| 12 | +func (app *application) createEvent(c *gin.Context) { |
| 13 | + |
| 14 | + var event database.Event |
| 15 | + |
| 16 | + if err := c.ShouldBindJSON(&event); err != nil { |
| 17 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 18 | + "error": err.Error(), |
| 19 | + }) |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + err := app.models.Events.insert(event) |
| 24 | + if err != nil { |
| 25 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 26 | + "error": "Failed to create event", |
| 27 | + }) |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + c.JSON(http.StatusCreated, event) |
| 32 | +} |
| 33 | + |
| 34 | +func (app *application) getAllEvent(c *gin.Context) { |
| 35 | + events, err := app.models.Events.GetAll() |
| 36 | + if err != nil { |
| 37 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 38 | + "error": "Failed to retireve events", |
| 39 | + }) |
| 40 | + return |
| 41 | + } |
| 42 | + c.JSON(http.StatusOK, events) |
| 43 | +} |
| 44 | + |
| 45 | +// get events |
| 46 | +func (app *application) getEvent(c *gin.Context) { |
| 47 | + id, err := strconv.Atoi(c.Param("id")) |
| 48 | + if err != nil { |
| 49 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 50 | + "error": "Invalid event Id", |
| 51 | + }) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + event, err := app.models.Events.Get(id) |
| 56 | + if err == nil { |
| 57 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 58 | + "error": "Event not found", |
| 59 | + }) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + if err != nil { |
| 64 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 65 | + "error": "Failed to retireve event", |
| 66 | + }) |
| 67 | + return |
| 68 | + } |
| 69 | + c.JSON(http.StatusCreated, event) |
| 70 | +} |
| 71 | + |
| 72 | +// update event |
| 73 | +func (app *application) updateEvent(c *gin.Context) { |
| 74 | + id, err := strconv.Atoi(c.Param("id")) |
| 75 | + if err != nil { |
| 76 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 77 | + "error": "Invalid event Id", |
| 78 | + }) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + existingEvent, err := app.models.Events.Get(id) |
| 83 | + if err != nil { |
| 84 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 85 | + "error": "Failed to retireve event", |
| 86 | + }) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + if existingEvent == nil { |
| 91 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 92 | + "erorr": "Event not found", |
| 93 | + }) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + updatedEvent := &database.Event{} |
| 98 | + |
| 99 | + if err := c.ShouldBind(updatedEvent); err != nil { |
| 100 | + c.JSON(http.StatusBadGateway, gin.H{ |
| 101 | + "error": err.Error(), |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + updatedEvent.Id = id |
| 106 | + |
| 107 | + if err := app.models.Events.Update(updatedEvent); err != nil { |
| 108 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 109 | + "error": "failed to update event", |
| 110 | + }) |
| 111 | + return |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func (app *application) deleteEvent(c *gin.Context) { |
| 116 | + id, err := strconv.Atoi(c.Param("id")) |
| 117 | + if err != nil { |
| 118 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 119 | + "error": "Invalid event Id", |
| 120 | + }) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + if err := app.models.Events.Delete(id); err != nil { |
| 125 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 126 | + "error": "Failed to delete event", |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | + c.JSON(http.StatusNoContent, nil) |
| 131 | +} |
0 commit comments