|
1 | 1 | package controllers
|
2 | 2 |
|
3 |
| -import "github.com/gin-gonic/gin" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "ecommerce-project/models" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | + "go.mongodb.org/mongo-driver/bson" |
| 12 | + "go.mongodb.org/mongo-driver/bson/primitive" |
| 13 | + "go.mongodb.org/mongo-driver/mongo" |
| 14 | +) |
4 | 15 |
|
5 | 16 | func AddAddress() gin.HandlerFunc {
|
| 17 | + return func(c *gin.Context) { |
| 18 | + user_id := c.Query("id") |
| 19 | + |
| 20 | + if user_id == "" { |
| 21 | + c.Header("Content-Type", "application/json") |
| 22 | + c.JSON(http.StatusNotFound, gin.H{"Error": "Invalid input"}) |
| 23 | + c.Abort() |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + address, err := primitive.ObjectIDFromHex(user_id) |
| 28 | + |
| 29 | + if err != nil { |
| 30 | + c.IndentedJSON(http.StatusInternalServerError, "Internal server error") |
| 31 | + } |
| 32 | + |
| 33 | + var addresses models.Address |
| 34 | + |
| 35 | + addresses.Address_ID = primitive.NewObjectID() |
| 36 | + |
| 37 | + if err = c.BindJSON(&addresses); err != nil { |
| 38 | + c.IndentedJSON(http.StatusNotAcceptable, err.Error()) |
| 39 | + } |
| 40 | + |
| 41 | + var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second) |
| 42 | + |
| 43 | + defer cancel() |
| 44 | + |
| 45 | + match_filter := bson.D{{Key: "$match", Value: bson.D{primitive.E{Key: "_id", Value: address}}}} |
| 46 | + unwind := bson.D{{Key: "$unwind", Value: bson.D{primitive.E{Key: "path", Value: "$address"}}}} |
| 47 | + group := bson.D{{Key: "$group", Value: bson.D{primitive.E{Key: "_id", Value: "$address_id"}, {Key: "count", Value: bson.D{primitive.E{Key: "$sum", Value: 1}}}}}} |
| 48 | + |
| 49 | + pointcursor, err := UserCollection.Aggregate(ctx, mongo.Pipeline{match_filter, unwind, group}) |
| 50 | + |
| 51 | + if err != nil { |
| 52 | + c.IndentedJSON(http.StatusInternalServerError, "Internal Server Error") |
| 53 | + } |
| 54 | + |
| 55 | + var addressinfo []bson.M |
| 56 | + |
| 57 | + if err = pointcursor.All(ctx, &addressinfo); err != nil { |
| 58 | + panic(err) |
| 59 | + } |
6 | 60 |
|
| 61 | + var size int32 |
| 62 | + |
| 63 | + for _, address_no := range addressinfo { |
| 64 | + count := address_no["count"] |
| 65 | + size = count.(int32) |
| 66 | + } |
| 67 | + |
| 68 | + if size > 2 { |
| 69 | + filter := bson.D{primitive.E{Key: "_id", Value: address}} |
| 70 | + update := bson.D{primitive.E{Key: "$push", Value: bson.D{primitive.E{Key: "address", Value: addresses}}}} |
| 71 | + |
| 72 | + _, err := UserCollection.UpdateOne(ctx, filter, update) |
| 73 | + |
| 74 | + if err != nil { |
| 75 | + fmt.Println(err) |
| 76 | + } |
| 77 | + |
| 78 | + } else { |
| 79 | + c.IndentedJSON(400, "Not Allowed") |
| 80 | + } |
| 81 | + |
| 82 | + defer cancel() |
| 83 | + |
| 84 | + ctx.Done() |
| 85 | + } |
7 | 86 | }
|
8 | 87 |
|
9 | 88 | func EditHomeAddress() gin.HandlerFunc {
|
| 89 | + return func(c *gin.Context) { |
| 90 | + user_id := c.Query("id") |
| 91 | + |
| 92 | + if user_id == "" { |
| 93 | + c.Header("Content-Type", "application/json") |
| 94 | + c.JSON(http.StatusNotFound, gin.H{"Error": "Invalid"}) |
| 95 | + c.Abort() |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + usert_id, err := primitive.ObjectIDFromHex(user_id) |
| 100 | + |
| 101 | + if err != nil { |
| 102 | + c.IndentedJSON(http.StatusInternalServerError, "Internal server error") |
| 103 | + } |
| 104 | + |
| 105 | + var editaddress models.Address |
| 106 | + if err := c.BindJSON(&editaddress); err != nil { |
| 107 | + c.IndentedJSON(http.StatusBadRequest, err.Error()) |
| 108 | + } |
| 109 | + |
| 110 | + var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second) |
| 111 | + |
| 112 | + defer cancel() |
10 | 113 |
|
| 114 | + filter := bson.D{primitive.E{Key: "_id", Value: usert_id}} |
| 115 | + |
| 116 | + update := bson.D{{Key: "$set", Value: bson.D{primitive.E{Key: "address.0.house_name", Value: editaddress.House}, {Key: "address.0.street_name", Value: editaddress.Street}, {Key: "address.0.city_name", Value: editaddress.City}, {Key: "address.0.pin_code", Value: editaddress.Pincode}}}} |
| 117 | + |
| 118 | + _, err = UserCollection.UpdateOne(ctx, filter, update) |
| 119 | + |
| 120 | + if err != nil { |
| 121 | + c.IndentedJSON(http.StatusInternalServerError, "Something went wrong") |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + defer cancel() |
| 126 | + |
| 127 | + ctx.Done() |
| 128 | + c.IndentedJSON(http.StatusOK, "Successfully updated address") |
| 129 | + } |
11 | 130 | }
|
12 | 131 |
|
13 | 132 | func EditWorkAddress() gin.HandlerFunc {
|
| 133 | + return func(c *gin.Context) { |
| 134 | + user_id := c.Query("id") |
| 135 | + |
| 136 | + if user_id == "" { |
| 137 | + c.Header("Content-Type", "application/json") |
| 138 | + c.JSON(http.StatusNotFound, gin.H{"Error": "Invalid"}) |
| 139 | + c.Abort() |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + usert_id, err := primitive.ObjectIDFromHex(user_id) |
| 144 | + |
| 145 | + if err != nil { |
| 146 | + c.IndentedJSON(http.StatusInternalServerError, "Internal server error") |
| 147 | + } |
| 148 | + |
| 149 | + var editaddress models.Address |
| 150 | + if err := c.BindJSON(&editaddress); err != nil { |
| 151 | + c.IndentedJSON(http.StatusBadRequest, err.Error()) |
| 152 | + } |
| 153 | + |
| 154 | + var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second) |
| 155 | + |
| 156 | + defer cancel() |
| 157 | + |
| 158 | + filter := bson.D{primitive.E{Key: "_id", Value: usert_id}} |
| 159 | + update := bson.D{{Key: "$set", Value: bson.D{primitive.E{Key: "address.1.house_name", Value: editaddress.House}, {Key: "address.1.street_name", Value: editaddress.Street}, {Key: "address.1.city_name", Value: editaddress.City}, {Key: "address.1.pin_code", Value: editaddress.Pincode}}}} |
| 160 | + |
| 161 | + _, err = UserCollection.UpdateOne(ctx, filter, update) |
| 162 | + |
| 163 | + if err != nil { |
| 164 | + c.IndentedJSON(http.StatusInternalServerError, "Something went wrong") |
| 165 | + return |
| 166 | + } |
14 | 167 |
|
| 168 | + defer cancel() |
| 169 | + |
| 170 | + ctx.Done() |
| 171 | + c.IndentedJSON(http.StatusOK, "Successfully updated address") |
| 172 | + } |
15 | 173 | }
|
16 | 174 |
|
17 | 175 | func DeleteAddress() gin.HandlerFunc {
|
| 176 | + return func(c *gin.Context) { |
| 177 | + user_id := c.Query("id") |
| 178 | + |
| 179 | + if user_id == "" { |
| 180 | + c.Header("Content-Type", "application/json") |
| 181 | + c.JSON(http.StatusNotFound, gin.H{"Error": "Invalid search index"}) |
| 182 | + c.Abort() |
| 183 | + return |
| 184 | + } |
| 185 | + |
| 186 | + addresses := make([]models.Address, 0) |
| 187 | + |
| 188 | + usert_id, err := primitive.ObjectIDFromHex(user_id) |
| 189 | + |
| 190 | + if err != nil { |
| 191 | + c.IndentedJSON(http.StatusInternalServerError, "Internal server error") |
| 192 | + } |
| 193 | + |
| 194 | + var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second) |
| 195 | + |
| 196 | + defer cancel() |
| 197 | + |
| 198 | + filter := bson.D{primitive.E{Key: "_id", Value: usert_id}} |
| 199 | + |
| 200 | + update := bson.D{{Key: "$set", Value: bson.D{primitive.E{Key: "address", Value: addresses}}}} |
| 201 | + |
| 202 | + _, err = UserCollection.UpdateOne(ctx, filter, update) |
| 203 | + |
| 204 | + if err != nil { |
| 205 | + c.IndentedJSON(http.StatusNotFound, "Wrong command") |
| 206 | + return |
| 207 | + } |
| 208 | + |
| 209 | + defer cancel() |
| 210 | + |
| 211 | + ctx.Done() |
18 | 212 |
|
| 213 | + c.IndentedJSON(http.StatusOK, "Successfully Deleted") |
| 214 | + } |
19 | 215 | }
|
0 commit comments