source: resTools_backend/backend/Services/RestaurantService.cs@ cc4db18

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

reservation module changes + contact module + menu module

  • Property mode set to 100644
File size: 2.6 KB
Line 
1using backend.Data;
2using backend.DTOs;
3using backend.Entities;
4using Microsoft.EntityFrameworkCore;
5
6namespace backend.Services
7{
8 public interface IRestaurantService
9 {
10 public Task CreateRestaurant(string name, int userId);
11 public Task<RestaurantResponse> GetRestaurant();
12 public Task UploadImage(IFormFile file);
13 public Task UpdateRestaurant(UpdateRestaurantRequest req);
14 }
15 public class RestaurantService : IRestaurantService
16 {
17 private readonly DataContext _context = null;
18
19 public RestaurantService(DataContext context)
20 {
21 _context = context;
22 }
23 public async Task CreateRestaurant(string name, int userId)
24 {
25 User user = await _context.Users.FindAsync(userId);
26 Restaurant restaurant = new Restaurant() { Name = name, Owner = user};
27 await _context.Restoraunts.AddAsync(restaurant);
28 await _context.SaveChangesAsync();
29 }
30
31 public async Task<RestaurantResponse> GetRestaurant()
32 {
33 RestaurantResponse res = await _context.Restoraunts
34 .Select(x => new RestaurantResponse()
35 {
36 Name = x.Name,
37 Address = x.Address,
38 Phone = x.Phone,
39 Base64Image = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(x.Image)),
40 Menu = x.Menu.Select(x => new MenuItemResponse()
41 {
42 Id = x.Id,
43 Title = x.Title,
44 Description = x.Description,
45 Price = x.Price
46 }).ToList()
47 })
48 .FirstOrDefaultAsync();
49 return res;
50 }
51
52 public async Task UpdateRestaurant(UpdateRestaurantRequest req)
53 {
54 var restaurant = await _context.Restoraunts.FirstOrDefaultAsync();
55 restaurant.Name = req.Name;
56 restaurant.Address = req.Address;
57 restaurant.Phone = req.Phone;
58 _context.Restoraunts.Update(restaurant);
59 await _context.SaveChangesAsync();
60 }
61
62 public async Task UploadImage(IFormFile file)
63 {
64 using (var memoryStream = new MemoryStream())
65 {
66 await file.CopyToAsync(memoryStream);
67 var restaurant = await _context.Restoraunts.FirstOrDefaultAsync();
68 restaurant.Image = memoryStream.ToArray();
69 _context.Restoraunts.Update(restaurant);
70 _context.SaveChanges();
71 }
72 }
73 }
74}
Note: See TracBrowser for help on using the repository browser.