Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 92cc34c

Browse filesBrowse files
committed
Cart-Order repos added
1 parent 1d9aa84 commit 92cc34c
Copy full SHA for 92cc34c

9 files changed

+220
-12
lines changed
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using AspnetRunBasics.Data;
2+
using AspnetRunBasics.Entities;
3+
using Microsoft.EntityFrameworkCore;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace AspnetRunBasics.Repositories
10+
{
11+
public class CartRepository : ICartRepository
12+
{
13+
protected readonly AspnetRunContext _dbContext;
14+
15+
public CartRepository(AspnetRunContext dbContext)
16+
{
17+
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
18+
}
19+
20+
public async Task<Cart> GetCartByUserName(string userName)
21+
{
22+
var cart = _dbContext.Carts
23+
.Include(c => c.Items)
24+
.FirstOrDefault(c => c.UserName == userName);
25+
26+
if (cart != null)
27+
return cart;
28+
29+
// if it is first attempt create new
30+
var newCart = new Cart
31+
{
32+
UserName = userName
33+
};
34+
35+
_dbContext.Carts.Add(newCart);
36+
await _dbContext.SaveChangesAsync();
37+
return newCart;
38+
}
39+
40+
public async Task AddItem(string userName, int productId)
41+
{
42+
var cart = await GetCartByUserName(userName);
43+
44+
cart.Items.Add(
45+
new CartItem
46+
{
47+
Id = productId,
48+
Color = "Black",
49+
Price = 10,
50+
Quantity = 1
51+
}
52+
);
53+
54+
_dbContext.Entry(cart).State = EntityState.Modified;
55+
await _dbContext.SaveChangesAsync();
56+
}
57+
58+
public async Task RemoveItem(int cartId, int cartItemId)
59+
{
60+
var cart = _dbContext.Carts
61+
.Include(c => c.Items)
62+
.FirstOrDefault(c => c.Id == cartId);
63+
64+
if (cart != null)
65+
{
66+
var removedItem = cart.Items.FirstOrDefault(x => x.Id == cartItemId);
67+
cart.Items.Remove(removedItem);
68+
69+
_dbContext.Entry(cart).State = EntityState.Modified;
70+
await _dbContext.SaveChangesAsync();
71+
}
72+
73+
}
74+
75+
public async Task ClearCart(string userName)
76+
{
77+
var cart = await GetCartByUserName(userName);
78+
79+
cart.Items.Clear();
80+
81+
_dbContext.Entry(cart).State = EntityState.Modified;
82+
await _dbContext.SaveChangesAsync();
83+
}
84+
}
85+
}
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using AspnetRunBasics.Data;
2+
using AspnetRunBasics.Entities;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace AspnetRunBasics.Repositories
7+
{
8+
public class ContactRepository : IContactRepository
9+
{
10+
protected readonly AspnetRunContext _dbContext;
11+
12+
public ContactRepository(AspnetRunContext dbContext)
13+
{
14+
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
15+
}
16+
17+
public async Task<Contact> SendMessage(Contact contact)
18+
{
19+
_dbContext.Contacts.Add(contact);
20+
await _dbContext.SaveChangesAsync();
21+
return contact;
22+
}
23+
24+
public async Task<Contact> Subscribe(string address)
25+
{
26+
// implement your business logic
27+
var newContact = new Contact();
28+
newContact.Email = address;
29+
newContact.Message = address;
30+
newContact.Name = address;
31+
32+
_dbContext.Contacts.Add(newContact);
33+
await _dbContext.SaveChangesAsync();
34+
35+
return newContact;
36+
}
37+
}
38+
}

‎AspnetRunBasics/Repositories/ICategoryRepository.cs

Copy file name to clipboardExpand all lines: AspnetRunBasics/Repositories/ICategoryRepository.cs
-11Lines changed: 0 additions & 11 deletions
This file was deleted.

‎AspnetRunBasics/Repositories/IOrderRepository.cs renamed to ‎AspnetRunBasics/Repositories/Interfaces/IOrderRepository.cs

Copy file name to clipboardExpand all lines: AspnetRunBasics/Repositories/Interfaces/IOrderRepository.cs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace AspnetRunBasics.Repositories
55
{
66
public interface IOrderRepository
77
{
8-
Task<Order> CheckOut(Order orderModel);
8+
Task<Order> CheckOut(Order order);
99
}
1010
}
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using AspnetRunBasics.Data;
2+
using AspnetRunBasics.Entities;
3+
using System;
4+
using System.Threading.Tasks;
5+
6+
namespace AspnetRunBasics.Repositories
7+
{
8+
public class OrderRepository : IOrderRepository
9+
{
10+
protected readonly AspnetRunContext _dbContext;
11+
12+
public OrderRepository(AspnetRunContext dbContext)
13+
{
14+
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
15+
}
16+
17+
public async Task<Order> CheckOut(Order order)
18+
{
19+
_dbContext.Orders.Add(order);
20+
await _dbContext.SaveChangesAsync();
21+
return order;
22+
}
23+
}
24+
}
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using AspnetRunBasics.Data;
6+
using AspnetRunBasics.Entities;
7+
using Microsoft.EntityFrameworkCore;
8+
9+
namespace AspnetRunBasics.Repositories
10+
{
11+
public class ProductRepository : IProductRepository
12+
{
13+
protected readonly AspnetRunContext _dbContext;
14+
15+
public ProductRepository(AspnetRunContext dbContext)
16+
{
17+
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
18+
}
19+
20+
public async Task<IEnumerable<Product>> GetProductListAsync()
21+
{
22+
return await _dbContext.Products.ToListAsync();
23+
}
24+
25+
public async Task<Product> GetProductByIdAsync(int id)
26+
{
27+
return await _dbContext.Products
28+
.Include(p => p.Category)
29+
.FirstOrDefaultAsync(p => p.Id == id);
30+
}
31+
32+
public async Task<IEnumerable<Product>> GetProductByNameAsync(string name)
33+
{
34+
return await _dbContext.Products
35+
.Include(p => p.Category)
36+
.Where(p => string.IsNullOrEmpty(name) || p.Name.ToLower().Contains(name.ToLower()))
37+
.OrderBy(p => p.Name)
38+
.ToListAsync();
39+
}
40+
41+
public async Task<IEnumerable<Product>> GetProductByCategoryAsync(int categoryId)
42+
{
43+
return await _dbContext.Products
44+
.Where(x => x.CategoryId == categoryId)
45+
.ToListAsync();
46+
}
47+
48+
public async Task<Product> AddAsync(Product product)
49+
{
50+
_dbContext.Products.Add(product);
51+
await _dbContext.SaveChangesAsync();
52+
return product;
53+
}
54+
55+
public async Task UpdateAsync(Product product)
56+
{
57+
_dbContext.Entry(product).State = EntityState.Modified;
58+
await _dbContext.SaveChangesAsync();
59+
}
60+
61+
public async Task DeleteAsync(Product product)
62+
{
63+
_dbContext.Products.Remove(product);
64+
await _dbContext.SaveChangesAsync();
65+
}
66+
67+
public async Task<IEnumerable<Category>> GetCategories()
68+
{
69+
return await _dbContext.Categories.ToListAsync();
70+
}
71+
}
72+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.