-
Notifications
You must be signed in to change notification settings - Fork 591
Expand file tree
/
Copy pathProductRepository.cs
More file actions
68 lines (53 loc) · 2.01 KB
/
Copy pathProductRepository.cs
File metadata and controls
68 lines (53 loc) · 2.01 KB
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
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Testing.Models;
namespace Testing
{
public class ProductRepository : IProductRepository
{
private readonly IDbConnection _conn;
public ProductRepository(IDbConnection conn)
{
_conn = conn;
}
public IEnumerable<Product> GetAllProducts()
{
return _conn.Query<Product>("SELECT * FROM adventureworks_products;");
}
public Product GetProduct(int id)
{
return _conn.QuerySingle<Product>("SELECT * FROM adventureworks_products WHERE ProductKey = @id",
new { id = id });
}
public void UpdateProduct(Product product)
{
_ = _conn.Execute("UPDATE adventureworks_products SET ProductName = @Name, ProductPrice = @Price WHERE ProductKey = @key",
new { name = product.ProductName, price = product.ProductPrice, key = product.ProductKey });
}
public void InsertProduct(Product productToInsert)
{
_conn.Execute("INSERT INTO adventureworks_products (NAME, PRICE, CATEGORYID) VALUES (@name, @price, @categoryID);",
new { name = productToInsert.ProductName, price = productToInsert.ProductPrice, categoryID = productToInsert.ProductKey });
}
public IEnumerable<Category> GetCategories()
{
return _conn.Query<Category>("SELECT * FROM adventureworks_territories;");
}
public Product AssignCategory()
{
var categoryList = GetCategories();
var product = new Product();
product.Categories = categoryList;
return product;
}
public void DeleteProduct(Product product)
{
_conn.Execute("DELETE FROM adventureworks_products WHERE ProductKey = @id;",
new { id = product.ProductKey });
}
}
}