-
Notifications
You must be signed in to change notification settings - Fork 30
/
DevBasketRepo.cs
159 lines (142 loc) · 4.91 KB
/
DevBasketRepo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using Microsoft.EntityFrameworkCore;
using MisterRobotoArigato.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace MisterRobotoArigato.Models
{
public class DevBasketRepo : IBasketRepo
{
private RobotoDbContext _context;
public DevBasketRepo(RobotoDbContext context)
{
_context = context;
}
/// <summary>
/// creates a basket for a user if the user tries to add an item to a basket if the user doesn't already have one
/// </summary>
/// <param name="basket"></param>
/// <returns></returns>
public async Task<HttpStatusCode> CreateBasket(Basket basket)
{
try
{
_context.Add(basket);
await _context.SaveChangesAsync();
}
catch
{
return HttpStatusCode.BadRequest;
}
return HttpStatusCode.Created;
}
/// <summary>
/// Adds a product to the basket
/// </summary>
/// <param name="email"></param>
/// <param name="product"></param>
/// <returns>successful HTTPS status code</returns>
public async Task<HttpStatusCode> AddProductToBasket(string email, Product product)
{
try
{
Basket datBasket = GetUserBasketByEmail(email).Result;
BasketItem datBasketItem = datBasket.BasketItems.FirstOrDefault(i => i.ProductID == product.ID);
if (datBasketItem != null)
{
datBasketItem.Quantity++;
await UpdateBasket(email, datBasketItem);
return HttpStatusCode.Created;
}
datBasketItem = new BasketItem
{
ProductID = product.ID,
ProductName = product.Name,
CustomerEmail = email,
Quantity = 1,
UnitPrice = product.Price,
ImgUrl = product.ImgUrl
};
await _context.AddAsync(datBasketItem);
await _context.SaveChangesAsync();
return HttpStatusCode.Created;
}
catch
{
return HttpStatusCode.BadRequest;
}
}
/// <summary>
/// Deletes a product from the basket
/// </summary>
/// <param name="basketItem"></param>
/// <returns>HTTP Status Code</returns>
public async Task<HttpStatusCode> DeleteProductFromBasket(BasketItem basketItem)
{
try
{
_context.Remove(basketItem);
await _context.SaveChangesAsync();
return HttpStatusCode.Created;
}
catch
{
return HttpStatusCode.BadRequest;
}
}
/// <summary>
/// Empties a basket
/// </summary>
/// <param name="basketItems"></param>
/// <returns>HTTP Status Code</returns>
public async Task<HttpStatusCode> ClearOutBasket(List<BasketItem> basketItems)
{
try
{
_context.RemoveRange(basketItems);
await _context.SaveChangesAsync();
return HttpStatusCode.Created;
}
catch
{
return HttpStatusCode.BadRequest;
}
}
/// <summary>
/// Gets the basket associated with a user.
/// the user is identified by their email
/// </summary>
/// <param name="email"></param>
/// <returns>the basket object associated with a user's email</returns>
public async Task<Basket> GetUserBasketByEmail(string email)
{
var prodInts = _context.BasketItems.Where(d => d.CustomerEmail == email).Select(p => p.ProductID);
List<BasketItem> demItems = _context.BasketItems.Where(d => d.CustomerEmail == email).ToList();
Basket datBasket = await _context.Baskets.FirstOrDefaultAsync(b => b.CustomerEmail == email);
if (datBasket != null)
datBasket.BasketItems = demItems;
return datBasket;
}
/// <summary>
/// Updates the items in the basket
/// </summary>
/// <param name="email"></param>
/// <param name="basketItem"></param>
/// <returns>HTTP Status code</returns>
public async Task<HttpStatusCode> UpdateBasket(string email, BasketItem basketItem)
{
try
{
_context.Update(basketItem);
await _context.SaveChangesAsync();
return HttpStatusCode.Created;
}
catch
{
return HttpStatusCode.BadRequest;
}
}
}
}