source: resTools_backend/backend/Services/MenuService.cs

Last change on this file was 13f1472, checked in by Danilo <danilo.najkov@…>, 2 years ago

vip functionallity + menu fields + alergens filtering + google/fb login + email queueing

  • Property mode set to 100644
File size: 1.9 KB
Line 
1using backend.Data;
2using backend.DTOs;
3using backend.Entities;
4using Microsoft.EntityFrameworkCore;
5
6namespace 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}
Note: See TracBrowser for help on using the repository browser.