Skip to content

Commit 38b3973

Browse files
committed
Migrate more system
Add scripts Seed data
1 parent bd3b9e0 commit 38b3973

20 files changed

+430
-27
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Linq;
3+
using Models.Db;
4+
5+
namespace Infrastructure.Managers
6+
{
7+
public class AccountManager
8+
{
9+
protected TitsDbContext Context { get; set; }
10+
11+
public AccountManager(TitsDbContext context)
12+
{
13+
Context = context;
14+
}
15+
16+
public Account CreateAccount(string login, string password, string username, WorkPoint mainWorkPoint)
17+
{
18+
if (Context.Accounts.Any(a => a.Login == login))
19+
{
20+
throw new ArgumentException($"Passed login '{login}' is already in use", nameof(login));
21+
}
22+
23+
Account account = new()
24+
{
25+
Login = login,
26+
Password = password,
27+
Username = username,
28+
MainWorkPoint = mainWorkPoint
29+
};
30+
31+
Context.Accounts.Add(account);
32+
Context.SaveChanges();
33+
return account;
34+
}
35+
}
36+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Linq;
3+
using Models.Db;
4+
5+
namespace Infrastructure.Managers
6+
{
7+
public class AccountRoleManager
8+
{
9+
private TitsDbContext _context;
10+
11+
public AccountRoleManager(TitsDbContext context)
12+
{
13+
_context = context;
14+
}
15+
16+
public void AddAccountRole(Account account, string roleEn)
17+
{
18+
var accountRole = _context.AccountRoles.FirstOrDefault(r => r.TitleEn == roleEn);
19+
20+
if (accountRole == null)
21+
{
22+
throw new ArgumentOutOfRangeException(nameof(roleEn), roleEn, null);
23+
}
24+
25+
_context.Entry(account).Collection(a => a.Roles).Load();
26+
27+
if (account.Roles.Any(r => r.RoleId == accountRole.Id))
28+
{
29+
throw new ArgumentOutOfRangeException($"Account already in role {roleEn}");
30+
}
31+
else
32+
{
33+
AccountToRole accountToRole = new() {Account = account, Role = accountRole};
34+
_context.AccountToRoles.Add(accountToRole);
35+
_context.SaveChanges();
36+
}
37+
}
38+
39+
public void RemoveAccountRole(Account account, string roleEn)
40+
{
41+
var accountRole = _context.AccountRoles.FirstOrDefault(r => r.TitleEn == roleEn);
42+
43+
if (accountRole == null)
44+
{
45+
throw new ArgumentOutOfRangeException(nameof(roleEn), roleEn, null);
46+
}
47+
48+
var accountToRole =
49+
_context.AccountToRoles.FirstOrDefault(role => role.Account == account && role.Role == accountRole);
50+
51+
if (accountToRole != null)
52+
{
53+
_context.AccountToRoles.Remove(accountToRole);
54+
_context.SaveChanges();
55+
}
56+
else
57+
{
58+
throw new ArgumentOutOfRangeException("Account is not in role");
59+
}
60+
}
61+
}
62+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Models.Db;
2+
3+
namespace Infrastructure.Managers
4+
{
5+
public class IngredientTemplateManager
6+
{
7+
private TitsDbContext Context { get; set; }
8+
9+
public IngredientTemplateManager(TitsDbContext context)
10+
{
11+
Context = context;
12+
}
13+
14+
public IngredientTemplate CreateIngredientTemplate(ProductTemplate productTemplate, string title, int weightGrams)
15+
{
16+
IngredientTemplate ingredientTemplate = new()
17+
{
18+
Title = title,
19+
Weight = weightGrams,
20+
Product = productTemplate
21+
};
22+
Context.IngredientTemplates.Add(ingredientTemplate);
23+
Context.SaveChanges();
24+
return ingredientTemplate;
25+
}
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Linq;
2+
using Models.Db;
3+
4+
namespace Infrastructure.Managers
5+
{
6+
public class ProductCategoryManager
7+
{
8+
protected TitsDbContext Context { get; set; }
9+
10+
public ProductCategoryManager(TitsDbContext context)
11+
{
12+
Context = context;
13+
}
14+
15+
public ProductCategory FindByTitleEn(string titleEn)
16+
{
17+
var productCategory = Context.ProductCategories.FirstOrDefault(t => t.TitleEn == titleEn);
18+
return productCategory;
19+
}
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using Models.Db;
3+
4+
namespace Infrastructure.Managers
5+
{
6+
public class ProductPackTemplateManager
7+
{
8+
protected TitsDbContext Context { get; set; }
9+
10+
public ProductPackTemplateManager(TitsDbContext context)
11+
{
12+
Context = context;
13+
}
14+
15+
public ProductPackTemplate CreateProductPack(string title, float price)
16+
{
17+
ProductPackTemplate productPackTemplate = new ProductPackTemplate()
18+
{
19+
Title = title,
20+
Price = price,
21+
Products = new List<ProductTemplate>()
22+
};
23+
Context.ProductPackTemplates.Add(productPackTemplate);
24+
Context.SaveChanges();
25+
return productPackTemplate;
26+
}
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections.Generic;
2+
using Models.Db;
3+
4+
namespace Infrastructure.Managers
5+
{
6+
public class ProductTemplateManager
7+
{
8+
private TitsDbContext Context { get; set; }
9+
10+
public ProductTemplateManager(TitsDbContext context)
11+
{
12+
Context = context;
13+
}
14+
15+
public ProductTemplate CreateProductTemplate(ProductPackTemplate productPackTemplate, string title, float price, ProductCategory productCategory)
16+
{
17+
ProductTemplate productTemplate = new()
18+
{
19+
Title = title,
20+
Price = price,
21+
ProductCategory = productCategory,
22+
Ingredients = new List<IngredientTemplate>(),
23+
ProductPack = productPackTemplate
24+
};
25+
Context.ProductTemplates.Add(productTemplate);
26+
Context.SaveChanges();
27+
return productTemplate;
28+
}
29+
}
30+
}

Infrastructure/Verbatims/AccountRolesVerbatim.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,21 @@ namespace Infrastructure.Verbatims
55
public class AccountRolesVerbatim
66
{
77
public const string Superuser = "Superuser";
8-
public const string Cook = "Cook";
9-
public const string CookHot = "CookHot";
10-
public const string Packer = "Packer";
11-
public const string DeliveryCashier = "DeliveryCashier";
12-
public const string Manager = "Manager";
138
public const string Client = "Client";
9+
public const string Courier = "Courier";
1410

1511
public static Dictionary<string, string> EnToRu = new()
1612
{
1713
{Superuser, "Супер-пользователь"},
18-
{Cook, "Повар"},
19-
{CookHot, "Повар горячего цеха"},
20-
{Packer, "Упаковщик"},
21-
{DeliveryCashier, "Кассир доставки"},
22-
{Manager, "Менеджер"},
23-
{Client, "Клиент"}
14+
{Client, "Клиент"},
15+
{Courier, "Курьер"},
2416
};
2517

2618
public static Dictionary<string, string> RuToEn = new()
2719
{
2820
{"Супер-пользователь", Superuser},
29-
{"Повар", Cook},
30-
{"Повар горячего цеха", CookHot},
31-
{"Упаковщик", Packer},
32-
{"Кассир доставки", DeliveryCashier},
33-
{"Менеджер", Manager},
34-
{"Клиент", Client}
21+
{"Клиент", Client},
22+
{"Курьер", Courier},
3523
};
3624
}
3725
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
cd ../
2-
export CONN_STR='Host=localhost;Port=55432;Database=DodoHack;Username=postgres;Password=root'
2+
export CONN_STR='Host=localhost;Port=55432;Database=TitsSystem;Username=postgres;Password=root'
33
echo y | dotnet ef database drop
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
cd ../
2-
export CONN_STR='Host=localhost;Port=5432;Database=DodoHack;Username=postgres;Password=root'
2+
export CONN_STR='Host=localhost;Port=5432;Database=TitsSystem;Username=postgres;Password=root'
33
dotnet ef migrations add improve -o Data/Migrations
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
cd ../
2-
export CONN_STR='Host=localhost;Port=5432;Database=DodoHack;Username=postgres;Password=root'
2+
export CONN_STR='Host=localhost;Port=5432;Database=TitsSystem;Username=postgres;Password=root'
33
echo y | dotnet ef database drop

0 commit comments

Comments
 (0)