Repository navigation
Expand file tree
/
Copy pathProductsRepository.cs
More file actions
58 lines (49 loc) · 1.73 KB
/
Copy pathProductsRepository.cs
File metadata and controls
58 lines (49 loc) · 1.73 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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Serializer;
namespace Data
{
public class ProductsRepository
{
private readonly string restService = ConfigurationManager.AppSettings["restService"];
private readonly string apiPath = ConfigurationManager.AppSettings["apiPath"];
readonly JsonNetSerialization serializer = new JsonNetSerialization();
readonly HttpHelpers httpHelpers = new HttpHelpers();
public Product New()
{
return new Product();
}
public void Update(Product product)
{
httpHelpers.HttpInvoke(SupportedHttpMethods.PUT, string.Format("{0}/{1}", restService, apiPath),
serializer.Serialize<Product>(product));
}
public List<Product> Get()
{
var content = httpHelpers.GetHttpContent(string.Format("{0}/{1}", restService, apiPath));
return serializer.DeSerialize<List<Product>>(content) as List<Product>;
}
public Product Get(string name) {
var url = string.Format("{0}/{1}?$filter=name eq'{2}'", restService, apiPath, name);
var content = httpHelpers.GetHttpContent(url);
var products = serializer.DeSerialize<IList<Product>>(content) as IList<Product>;
return products[0];
}
public Product Get(int id)
{
var content = httpHelpers.GetHttpContent(string.Format("{0}/{1}{2}", restService, apiPath, id));
return serializer.DeSerialize<Product>(content) as Product;
}
public void Create(Product product)
{
httpHelpers.HttpInvoke(SupportedHttpMethods.POST, string.Format("{0}{1}", restService, apiPath),
serializer.Serialize<Product>(product));
}
public void Delete(int id)
{
httpHelpers.HttpInvoke(SupportedHttpMethods.DELETE, string.Format("{0}/{1}{2}", restService, apiPath, id));
}
}
}