Skip to content

Commit ceb4aa4

Browse files
committed
Rename templates, implement create menu
1 parent 9aae38a commit ceb4aa4

12 files changed

+915
-18
lines changed

Infrastructure/Data/Migrations/20210409202243_rename-templates.Designer.cs

+678
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
namespace Infrastructure.Data.Migrations
4+
{
5+
public partial class renametemplates : Migration
6+
{
7+
protected override void Up(MigrationBuilder migrationBuilder)
8+
{
9+
migrationBuilder.DropForeignKey(
10+
name: "FK_IngredientTemplates_ProductTemplates_ProductId",
11+
table: "IngredientTemplates");
12+
13+
migrationBuilder.DropForeignKey(
14+
name: "FK_ProductTemplates_ProductCategories_ProductCategoryId",
15+
table: "ProductTemplates");
16+
17+
migrationBuilder.DropForeignKey(
18+
name: "FK_ProductTemplates_ProductPackTemplates_ProductPackId",
19+
table: "ProductTemplates");
20+
21+
migrationBuilder.DropPrimaryKey(
22+
name: "PK_ProductTemplates",
23+
table: "ProductTemplates");
24+
25+
migrationBuilder.DropPrimaryKey(
26+
name: "PK_ProductPackTemplates",
27+
table: "ProductPackTemplates");
28+
29+
migrationBuilder.DropPrimaryKey(
30+
name: "PK_IngredientTemplates",
31+
table: "IngredientTemplates");
32+
33+
migrationBuilder.RenameTable(
34+
name: "ProductTemplates",
35+
newName: "MenuProducts");
36+
37+
migrationBuilder.RenameTable(
38+
name: "ProductPackTemplates",
39+
newName: "MenuProductPacks");
40+
41+
migrationBuilder.RenameTable(
42+
name: "IngredientTemplates",
43+
newName: "MenuIngredients");
44+
45+
migrationBuilder.RenameIndex(
46+
name: "IX_ProductTemplates_ProductPackId",
47+
table: "MenuProducts",
48+
newName: "IX_MenuProducts_ProductPackId");
49+
50+
migrationBuilder.RenameIndex(
51+
name: "IX_ProductTemplates_ProductCategoryId",
52+
table: "MenuProducts",
53+
newName: "IX_MenuProducts_ProductCategoryId");
54+
55+
migrationBuilder.RenameIndex(
56+
name: "IX_IngredientTemplates_ProductId",
57+
table: "MenuIngredients",
58+
newName: "IX_MenuIngredients_ProductId");
59+
60+
migrationBuilder.AddPrimaryKey(
61+
name: "PK_MenuProducts",
62+
table: "MenuProducts",
63+
column: "Id");
64+
65+
migrationBuilder.AddPrimaryKey(
66+
name: "PK_MenuProductPacks",
67+
table: "MenuProductPacks",
68+
column: "Id");
69+
70+
migrationBuilder.AddPrimaryKey(
71+
name: "PK_MenuIngredients",
72+
table: "MenuIngredients",
73+
column: "Id");
74+
75+
migrationBuilder.AddForeignKey(
76+
name: "FK_MenuIngredients_MenuProducts_ProductId",
77+
table: "MenuIngredients",
78+
column: "ProductId",
79+
principalTable: "MenuProducts",
80+
principalColumn: "Id",
81+
onDelete: ReferentialAction.Cascade);
82+
83+
migrationBuilder.AddForeignKey(
84+
name: "FK_MenuProducts_MenuProductPacks_ProductPackId",
85+
table: "MenuProducts",
86+
column: "ProductPackId",
87+
principalTable: "MenuProductPacks",
88+
principalColumn: "Id",
89+
onDelete: ReferentialAction.Cascade);
90+
91+
migrationBuilder.AddForeignKey(
92+
name: "FK_MenuProducts_ProductCategories_ProductCategoryId",
93+
table: "MenuProducts",
94+
column: "ProductCategoryId",
95+
principalTable: "ProductCategories",
96+
principalColumn: "Id",
97+
onDelete: ReferentialAction.Cascade);
98+
}
99+
100+
protected override void Down(MigrationBuilder migrationBuilder)
101+
{
102+
migrationBuilder.DropForeignKey(
103+
name: "FK_MenuIngredients_MenuProducts_ProductId",
104+
table: "MenuIngredients");
105+
106+
migrationBuilder.DropForeignKey(
107+
name: "FK_MenuProducts_MenuProductPacks_ProductPackId",
108+
table: "MenuProducts");
109+
110+
migrationBuilder.DropForeignKey(
111+
name: "FK_MenuProducts_ProductCategories_ProductCategoryId",
112+
table: "MenuProducts");
113+
114+
migrationBuilder.DropPrimaryKey(
115+
name: "PK_MenuProducts",
116+
table: "MenuProducts");
117+
118+
migrationBuilder.DropPrimaryKey(
119+
name: "PK_MenuProductPacks",
120+
table: "MenuProductPacks");
121+
122+
migrationBuilder.DropPrimaryKey(
123+
name: "PK_MenuIngredients",
124+
table: "MenuIngredients");
125+
126+
migrationBuilder.RenameTable(
127+
name: "MenuProducts",
128+
newName: "ProductTemplates");
129+
130+
migrationBuilder.RenameTable(
131+
name: "MenuProductPacks",
132+
newName: "ProductPackTemplates");
133+
134+
migrationBuilder.RenameTable(
135+
name: "MenuIngredients",
136+
newName: "IngredientTemplates");
137+
138+
migrationBuilder.RenameIndex(
139+
name: "IX_MenuProducts_ProductPackId",
140+
table: "ProductTemplates",
141+
newName: "IX_ProductTemplates_ProductPackId");
142+
143+
migrationBuilder.RenameIndex(
144+
name: "IX_MenuProducts_ProductCategoryId",
145+
table: "ProductTemplates",
146+
newName: "IX_ProductTemplates_ProductCategoryId");
147+
148+
migrationBuilder.RenameIndex(
149+
name: "IX_MenuIngredients_ProductId",
150+
table: "IngredientTemplates",
151+
newName: "IX_IngredientTemplates_ProductId");
152+
153+
migrationBuilder.AddPrimaryKey(
154+
name: "PK_ProductTemplates",
155+
table: "ProductTemplates",
156+
column: "Id");
157+
158+
migrationBuilder.AddPrimaryKey(
159+
name: "PK_ProductPackTemplates",
160+
table: "ProductPackTemplates",
161+
column: "Id");
162+
163+
migrationBuilder.AddPrimaryKey(
164+
name: "PK_IngredientTemplates",
165+
table: "IngredientTemplates",
166+
column: "Id");
167+
168+
migrationBuilder.AddForeignKey(
169+
name: "FK_IngredientTemplates_ProductTemplates_ProductId",
170+
table: "IngredientTemplates",
171+
column: "ProductId",
172+
principalTable: "ProductTemplates",
173+
principalColumn: "Id",
174+
onDelete: ReferentialAction.Cascade);
175+
176+
migrationBuilder.AddForeignKey(
177+
name: "FK_ProductTemplates_ProductCategories_ProductCategoryId",
178+
table: "ProductTemplates",
179+
column: "ProductCategoryId",
180+
principalTable: "ProductCategories",
181+
principalColumn: "Id",
182+
onDelete: ReferentialAction.Cascade);
183+
184+
migrationBuilder.AddForeignKey(
185+
name: "FK_ProductTemplates_ProductPackTemplates_ProductPackId",
186+
table: "ProductTemplates",
187+
column: "ProductPackId",
188+
principalTable: "ProductPackTemplates",
189+
principalColumn: "Id",
190+
onDelete: ReferentialAction.Cascade);
191+
}
192+
}
193+
}

Infrastructure/Data/Migrations/DodoHackDbContextModelSnapshot.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
152152

153153
b.HasIndex("ProductId");
154154

155-
b.ToTable("IngredientTemplates");
155+
b.ToTable("MenuIngredients");
156156
});
157157

158158
modelBuilder.Entity("Models.Db.MenuProduct", b =>
@@ -180,7 +180,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
180180

181181
b.HasIndex("ProductPackId");
182182

183-
b.ToTable("ProductTemplates");
183+
b.ToTable("MenuProducts");
184184
});
185185

186186
modelBuilder.Entity("Models.Db.MenuProductPack", b =>
@@ -198,7 +198,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
198198

199199
b.HasKey("Id");
200200

201-
b.ToTable("ProductPackTemplates");
201+
b.ToTable("MenuProductPacks");
202202
});
203203

204204
modelBuilder.Entity("Models.Db.Order", b =>

Infrastructure/Managers/IngredientTemplateManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public MenuIngredient CreateIngredientTemplate(MenuProduct menuProduct, string t
1919
Weight = weightGrams,
2020
MenuProduct = menuProduct
2121
};
22-
Context.IngredientTemplates.Add(menuIngredient);
22+
Context.MenuIngredients.Add(menuIngredient);
2323
Context.SaveChanges();
2424
return menuIngredient;
2525
}

Infrastructure/Managers/ProductPackTemplateManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public MenuProductPack CreateProductPack(string title, float price)
2020
Price = price,
2121
Products = new List<MenuProduct>()
2222
};
23-
Context.ProductPackTemplates.Add(menuProductPack);
23+
Context.MenuProductPacks.Add(menuProductPack);
2424
Context.SaveChanges();
2525
return menuProductPack;
2626
}

Infrastructure/Managers/ProductTemplateManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public MenuProduct CreateProductTemplate(MenuProductPack menuProductPack, string
2222
Ingredients = new List<MenuIngredient>(),
2323
MenuProductPack = menuProductPack
2424
};
25-
Context.ProductTemplates.Add(menuProduct);
25+
Context.MenuProducts.Add(menuProduct);
2626
Context.SaveChanges();
2727
return menuProduct;
2828
}

Infrastructure/TitsDbContext.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3939

4040
public DbSet<ProductCategory> ProductCategories { get; set; }
4141

42-
public DbSet<MenuIngredient> IngredientTemplates { get; set; }
43-
public DbSet<MenuProduct> ProductTemplates { get; set; }
44-
public DbSet<MenuProductPack> ProductPackTemplates { get; set; }
42+
public DbSet<MenuIngredient> MenuIngredients { get; set; }
43+
public DbSet<MenuProduct> MenuProducts { get; set; }
44+
public DbSet<MenuProductPack> MenuProductPacks { get; set; }
4545

4646
public DbSet<OrderIngredient> OrderIngredients { get; set; }
4747
public DbSet<OrderProduct> OrderProducts { get; set; }
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
cd ../
22
export CONN_STR='Host=localhost;Port=5432;Database=TitsSystem;Username=postgres;Password=root'
3-
dotnet ef migrations add models-bringup -o Data/Migrations
3+
dotnet ef migrations add rename-templates -o Data/Migrations
44
read -p "Press enter to continue"

Models/Dtos/Requests/CreateIngredientDto.cs

+2
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ public class CreateIngredientDto
55
public string Title { get; set; }
66

77
public int Weight { get; set; }
8+
9+
public long ProductId { get; set; }
810
}
911
}

Models/Dtos/Requests/CreateProductDto.cs

+2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ public class CreateProductDto
1111
public ICollection<long> Ingredients { get; set; }
1212

1313
public long ProductCategoryId { get; set; }
14+
15+
public long ProductPackId { get; set; }
1416
}
1517
}

TitsAPI/Areas/API/MenuController.cs

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Threading.Tasks;
3+
using AutoMapper;
34
using Infrastructure;
45
using Microsoft.AspNetCore.Mvc;
6+
using Models.Db;
7+
using Models.DTOs.Misc;
58
using Models.DTOs.Requests;
69
using TitsAPI.Controllers;
710
using TitsAPI.Filters;
@@ -10,29 +13,48 @@ namespace TitsAPI.Areas.API
1013
{
1114
public class MenuController : TitsController
1215
{
13-
public MenuController(TitsDbContext context) : base(context)
16+
private IMapper _mapper;
17+
18+
public MenuController(TitsDbContext context, IMapper mapper) : base(context)
1419
{
20+
_mapper = mapper;
1521
}
1622

1723
[HttpPost]
1824
[TypeFilter(typeof(CheckAuthTokenFilter))]
19-
public async Task CreateIngredient([FromBody] CreateIngredientDto createIngredientDto)
25+
public async Task<ActionResult<CreatedDto>> CreateIngredient([FromBody] CreateIngredientDto createIngredientDto)
2026
{
21-
throw new NotImplementedException();
27+
var menuIngredient = _mapper.Map<MenuIngredient>(createIngredientDto);
28+
29+
await Context.MenuIngredients.AddAsync(menuIngredient);
30+
await Context.SaveChangesAsync();
31+
32+
return new CreatedDto(menuIngredient.Id);
2233
}
2334

2435
[HttpPost]
2536
[TypeFilter(typeof(CheckAuthTokenFilter))]
26-
public async Task CreateProduct([FromBody] CreateProductDto createProductDto)
37+
public async Task<ActionResult<CreatedDto>> CreateProduct([FromBody] CreateProductDto createProductDto)
2738
{
28-
throw new NotImplementedException();
39+
var menuProduct = _mapper.Map<MenuProduct>(createProductDto);
40+
41+
await Context.MenuProducts.AddAsync(menuProduct);
42+
await Context.SaveChangesAsync();
43+
44+
return new CreatedDto(menuProduct.Id);
2945
}
3046

3147
[HttpPost]
3248
[TypeFilter(typeof(CheckAuthTokenFilter))]
33-
public async Task CreateProductPack([FromBody] CreateProductPackDto createProductPackDto)
49+
public async Task<ActionResult<CreatedDto>> CreateProductPack(
50+
[FromBody] CreateProductPackDto createProductPackDto)
3451
{
35-
throw new NotImplementedException();
52+
var menuProductPack = _mapper.Map<MenuProductPack>(createProductPackDto);
53+
54+
await Context.MenuProductPacks.AddAsync(menuProductPack);
55+
await Context.SaveChangesAsync();
56+
57+
return new CreatedDto(menuProductPack.Id);
3658
}
3759
}
3860
}

TitsAPI/Areas/API/OrderController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async Task<ActionResult<CreatedDto>> Create([FromBody] CreateOrderDto cre
5656

5757
var productPackTemplateIds = createOrderDto.ProductPackTemplateIds;
5858

59-
var productPackTemplates = await Context.ProductPackTemplates
59+
var productPackTemplates = await Context.MenuProductPacks
6060
.Include(ppt => ppt.Products)
6161
.ThenInclude(pt => pt.Ingredients)
6262
.Where(ppt => productPackTemplateIds.Contains(ppt.Id))

0 commit comments

Comments
 (0)