1 | using backend.Data;
|
---|
2 | using backend.DTOs;
|
---|
3 | using backend.Entities;
|
---|
4 | using Microsoft.EntityFrameworkCore;
|
---|
5 |
|
---|
6 | namespace backend.Services
|
---|
7 | {
|
---|
8 | public interface IMenuService
|
---|
9 | {
|
---|
10 | public Task AddMenu(CreateMenuItemRequest menu);
|
---|
11 | public Task RemoveMenu(int id);
|
---|
12 | public Task UploadImage(int id, IFormFile file);
|
---|
13 | }
|
---|
14 | public class MenuService : IMenuService
|
---|
15 | {
|
---|
16 | private readonly DataContext _context = null;
|
---|
17 | public MenuService(DataContext context)
|
---|
18 | {
|
---|
19 | _context = context;
|
---|
20 | }
|
---|
21 | public async Task AddMenu(CreateMenuItemRequest menu)
|
---|
22 | {
|
---|
23 | var res = await _context.Restoraunts.FirstOrDefaultAsync();
|
---|
24 | if(res.Menu == null)
|
---|
25 | {
|
---|
26 | res.Menu = new List<MenuItem>();
|
---|
27 | }
|
---|
28 | res.Menu.Add(new MenuItem()
|
---|
29 | {
|
---|
30 | Title = menu.Title,
|
---|
31 | Description = menu.Description,
|
---|
32 | Price = menu.Price,
|
---|
33 | Alergens = menu.Alergens,
|
---|
34 | IsVipOnly = menu.IsVipOnly,
|
---|
35 | Image = Array.Empty<byte>()
|
---|
36 | }) ;
|
---|
37 | _context.Restoraunts.Update(res);
|
---|
38 | await _context.SaveChangesAsync();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public async Task RemoveMenu(int id)
|
---|
42 | {
|
---|
43 | var res = await _context.Restoraunts.Include(x => x.Menu).FirstOrDefaultAsync();
|
---|
44 | res.Menu = res.Menu.Where(x => x.Id != id).ToList();
|
---|
45 | _context.Restoraunts.Update(res);
|
---|
46 | await _context.SaveChangesAsync();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public async Task UploadImage(int id, IFormFile file)
|
---|
50 | {
|
---|
51 | using (var memoryStream = new MemoryStream())
|
---|
52 | {
|
---|
53 | await file.CopyToAsync(memoryStream);
|
---|
54 | var menuItem = await _context.MenuItems.FindAsync(id);
|
---|
55 | menuItem.Image = memoryStream.ToArray();
|
---|
56 | _context.MenuItems.Update(menuItem);
|
---|
57 | _context.SaveChanges();
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|