-
Notifications
You must be signed in to change notification settings - Fork 122
/
ProductCategoryService.cs
139 lines (115 loc) · 5.27 KB
/
ProductCategoryService.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using KatlaSport.DataAccess;
using KatlaSport.DataAccess.ProductCatalogue;
using DbProductCategory = KatlaSport.DataAccess.ProductCatalogue.ProductCategory;
namespace KatlaSport.Services.ProductManagement
{
/// <summary>
/// Represents a product category service.
/// </summary>
public class ProductCategoryService : IProductCategoryService
{
private readonly IProductCatalogueContext _context;
private readonly IUserContext _userContext;
/// <summary>
/// Initializes a new instance of the <see cref="ProductCategoryService"/> class with specified <see cref="IProductCatalogueContext"/>.
/// </summary>
/// <param name="context">A <see cref="IProductCatalogueContext"/>.</param>
/// <param name="userContext">A <see cref="IUserContext"/>.</param>
public ProductCategoryService(IProductCatalogueContext context, IUserContext userContext)
{
_context = context ?? throw new ArgumentNullException();
_userContext = userContext ?? throw new ArgumentNullException();
}
/// <inheritdoc/>
public async Task<List<ProductCategoryListItem>> GetCategoriesAsync(int start, int amount)
{
var dbCategories = await _context.Categories.OrderBy(c => c.Id).Skip(start).Take(amount).ToListAsync();
var categories = dbCategories.Select(c => Mapper.Map<ProductCategoryListItem>(c)).ToList();
// TODO STEP 3 - Add code for setting "ProductCount" property for each category here.
return categories;
}
/// <inheritdoc/>
public async Task<ProductCategory> GetCategoryAsync(int categoryId)
{
var dbCategories = await _context.Categories.Where(p => p.Id == categoryId).ToArrayAsync();
if (dbCategories.Length == 0)
{
throw new RequestedResourceNotFoundException();
}
return Mapper.Map<DbProductCategory, ProductCategory>(dbCategories[0]);
}
/// <inheritdoc/>
public async Task<ProductCategory> CreateCategoryAsync(UpdateProductCategoryRequest createRequest)
{
var dbCategories = await _context.Categories.Where(c => c.Code == createRequest.Code).ToArrayAsync();
if (dbCategories.Length > 0)
{
throw new RequestedResourceHasConflictException("code");
}
var dbCategory = Mapper.Map<UpdateProductCategoryRequest, DbProductCategory>(createRequest);
dbCategory.CreatedBy = _userContext.UserId;
dbCategory.LastUpdatedBy = _userContext.UserId;
_context.Categories.Add(dbCategory);
await _context.SaveChangesAsync();
return Mapper.Map<ProductCategory>(dbCategory);
}
/// <inheritdoc/>
public async Task<ProductCategory> UpdateCategoryAsync(int categoryId, UpdateProductCategoryRequest updateRequest)
{
var dbCategories = await _context.Categories.Where(c => c.Code == updateRequest.Code && c.Id != categoryId).ToArrayAsync();
if (dbCategories.Length > 0)
{
throw new RequestedResourceHasConflictException("code");
}
dbCategories = await _context.Categories.Where(c => c.Id == categoryId).ToArrayAsync();
var dbCategory = dbCategories.FirstOrDefault();
if (dbCategory == null)
{
throw new RequestedResourceNotFoundException();
}
Mapper.Map(updateRequest, dbCategory);
dbCategory.LastUpdatedBy = _userContext.UserId;
await _context.SaveChangesAsync();
dbCategories = await _context.Categories.Where(c => c.Id == categoryId).ToArrayAsync();
return dbCategories.Select(c => Mapper.Map<ProductCategory>(c)).FirstOrDefault();
}
/// <inheritdoc/>
public async Task DeleteCategoryAsync(int categoryId)
{
var dbCategories = await _context.Categories.Where(c => categoryId == c.Id).ToArrayAsync();
if (dbCategories.Length == 0)
{
throw new RequestedResourceNotFoundException();
}
var dbCategory = dbCategories[0];
if (dbCategory.IsDeleted == false)
{
throw new RequestedResourceHasConflictException();
}
_context.Categories.Remove(dbCategory);
await _context.SaveChangesAsync();
}
/// <inheritdoc/>
public async Task SetStatusAsync(int categoryId, bool deletedStatus)
{
var dbCategories = await _context.Categories.Where(c => categoryId == c.Id).ToArrayAsync();
if (dbCategories.Length == 0)
{
throw new RequestedResourceNotFoundException();
}
var dbCategory = dbCategories[0];
if (dbCategory.IsDeleted != deletedStatus)
{
dbCategory.IsDeleted = deletedStatus;
dbCategory.LastUpdated = DateTime.UtcNow;
dbCategory.LastUpdatedBy = _userContext.UserId;
await _context.SaveChangesAsync();
}
}
}
}