Skip to content

Commit b2f18d0

Browse files
author
=
committed
Complete middleware, tokengen, address. Fix main and routes. Working on cart
1 parent 00af84c commit b2f18d0

File tree

10 files changed

+651
-44
lines changed

10 files changed

+651
-44
lines changed

controllers/address.go

+197-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,215 @@
11
package controllers
22

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+
)
415

516
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+
}
660

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+
}
786
}
887

988
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()
10113

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+
}
11130
}
12131

13132
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+
}
14167

168+
defer cancel()
169+
170+
ctx.Done()
171+
c.IndentedJSON(http.StatusOK, "Successfully updated address")
172+
}
15173
}
16174

17175
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()
18212

213+
c.IndentedJSON(http.StatusOK, "Successfully Deleted")
214+
}
19215
}

controllers/cart.go

+112-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package controllers
33
import (
44
"context"
55
"ecommerce-project/database"
6+
"ecommerce-project/models"
67
"errors"
78
"log"
89
"net/http"
910
"time"
1011

1112
"github.com/gin-gonic/gin"
13+
"go.mongodb.org/mongo-driver/bson"
1214
"go.mongodb.org/mongo-driver/bson/primitive"
1315
"go.mongodb.org/mongo-driver/mongo"
1416
)
@@ -112,13 +114,121 @@ func (app *Application) RemoveFromCart() gin.HandlerFunc {
112114
}
113115

114116
func GetItomFromCart() gin.HandlerFunc {
117+
return func(c *gin.Context) {
118+
user_id := c.Query("id")
119+
120+
// Checking user_id input is empty string or not
121+
if user_id == "" {
122+
c.Header("Content-Type", "application/json")
123+
c.JSON(http.StatusNotFound, gin.H{"Error": "Invalid id"})
124+
c.Abort()
125+
return
126+
}
127+
128+
usert_id, _ := primitive.ObjectIDFromHex(user_id)
129+
130+
var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second)
131+
132+
defer cancel()
133+
134+
var filledCart models.User
135+
136+
err := UserCollection.FindOne(ctx, bson.D{primitive.E{Key: "_id", Value: usert_id}}).Decode(&filledCart)
137+
138+
if err != nil {
139+
log.Println(err)
140+
c.IndentedJSON(http.StatusInternalServerError, "Not Found")
141+
return
142+
}
143+
144+
filter_match := bson.D{{Key: "$match", Value: bson.D{primitive.E{Key: "_id", Value: usert_id}}}}
145+
unwind := bson.D{{Key: "$unwind", Value: bson.D{primitive.E{Key: "path", Value: "$usercart"}}}}
146+
grouping := bson.D{{Key: "$group", Value: bson.D{primitive.E{Key: "_id", Value: "$_id"}, {Key: "total", Value: bson.D{primitive.E{Key: "$sum", Value: ""}}}}}}
147+
148+
pointcursor, err := UserCollection.Aggregate(ctx, mongo.Pipeline{filter_match, unwind, grouping})
149+
150+
if err != nil {
151+
log.Println(err)
152+
}
153+
154+
var listing []bson.M
155+
156+
if err = pointcursor.All(ctx, &listing); err != nil {
157+
log.Println(err)
158+
c.AbortWithStatus(http.StatusInternalServerError)
159+
}
160+
161+
for _, json := range listing {
162+
c.IndentedJSON(http.StatusOK, json["total"])
163+
c.IndentedJSON(http.StatusOK, filledCart.UserCart)
164+
}
115165

166+
ctx.Done()
167+
}
116168
}
117169

118-
func BuyFromCart() gin.HandlerFunc {
170+
func (app *Application) BuyFromCart() gin.HandlerFunc {
171+
return func(c *gin.Context) {
172+
userQueryID := c.Query("id")
173+
174+
if userQueryID == "" {
175+
log.Panicln("UserId is empty")
176+
_ = c.AbortWithError(http.StatusBadRequest, errors.New("UserId is empty"))
177+
}
178+
179+
var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second)
180+
181+
defer cancel()
182+
183+
err := database.BuyItemFromCart(ctx, app.userCollection, userQueryID)
119184

185+
if err != nil {
186+
c.IndentedJSON(http.StatusInternalServerError, err)
187+
}
188+
189+
c.IndentedJSON(200, "Succesfully placed the item")
190+
}
120191
}
121192

122-
func InstantBuy() gin.HandlerFunc {
193+
func (app *Application) InstantBuy() gin.HandlerFunc {
194+
return func(c *gin.Context) {
195+
productQueryID := c.Query("id")
196+
197+
if productQueryID == "" {
198+
log.Println(("Product id is empty"))
199+
200+
_ = c.AbortWithError(http.StatusBadRequest, errors.New("Product ID is empty"))
201+
return
202+
}
203+
204+
userQueryID := c.Query("userID")
205+
206+
if userQueryID == "" {
207+
log.Println("UserId is empty")
208+
209+
_ = c.AbortWithError(http.StatusBadRequest, errors.New("USer ID is empty"))
210+
return
211+
}
123212

213+
productID, err := primitive.ObjectIDFromHex(productQueryID)
214+
215+
if err != nil {
216+
log.Println(err)
217+
218+
c.AbortWithStatus(http.StatusInternalServerError)
219+
return
220+
}
221+
222+
var ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
223+
224+
defer cancel()
225+
226+
err = database.InstantBuyer(ctx, app.prodCollection, app.userCollection, productID, userQueryID)
227+
228+
if err != nil {
229+
c.IndentedJSON(http.StatusInternalServerError, err)
230+
}
231+
232+
c.IndentedJSON(200, "Successfully placed order")
233+
}
124234
}

0 commit comments

Comments
 (0)