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 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 | .Include(x => x.Menu)
|
---|
35 | .Include(x => x.Reviews).ThenInclude(x => x.User)
|
---|
36 | .Select(x => new RestaurantResponse()
|
---|
37 | {
|
---|
38 | Name = x.Name,
|
---|
39 | Address = x.Address,
|
---|
40 | Phone = x.Phone,
|
---|
41 | Base64Image = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(x.Image)),
|
---|
42 | Menu = x.Menu.Select(x => new MenuItemResponse()
|
---|
43 | {
|
---|
44 | Id = x.Id,
|
---|
45 | Title = x.Title,
|
---|
46 | Description = x.Description,
|
---|
47 | Price = x.Price,
|
---|
48 | Alergens = x.Alergens,
|
---|
49 | Image = String.Format("data:image/png;base64,{0}", Convert.ToBase64String(x.Image)),
|
---|
50 | IsVipOnly = x.IsVipOnly
|
---|
51 | }).ToList(),
|
---|
52 | Reviews = x.Reviews.Select(x => new ReviewResponse()
|
---|
53 | {
|
---|
54 | CreatedAt = x.CreatedAt,
|
---|
55 | Description = x.Description,
|
---|
56 | Id = x.Id,
|
---|
57 | Stars = x.Stars,
|
---|
58 | Title = x.Title,
|
---|
59 | Username = x.User == null ? "Anonymous" : x.User.Email
|
---|
60 | }).ToList()
|
---|
61 | })
|
---|
62 | .FirstOrDefaultAsync();
|
---|
63 | var reviews = await _context.Reviews.ToListAsync();
|
---|
64 | res.AverageReview = reviews.Select(x => x.Stars).Sum();
|
---|
65 | return res;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public async Task UpdateRestaurant(UpdateRestaurantRequest req)
|
---|
69 | {
|
---|
70 | var restaurant = await _context.Restoraunts.FirstOrDefaultAsync();
|
---|
71 | restaurant.Name = req.Name;
|
---|
72 | restaurant.Address = req.Address;
|
---|
73 | restaurant.Phone = req.Phone;
|
---|
74 | _context.Restoraunts.Update(restaurant);
|
---|
75 | await _context.SaveChangesAsync();
|
---|
76 | }
|
---|
77 |
|
---|
78 | public async Task UploadImage(IFormFile file)
|
---|
79 | {
|
---|
80 | using (var memoryStream = new MemoryStream())
|
---|
81 | {
|
---|
82 | await file.CopyToAsync(memoryStream);
|
---|
83 | var restaurant = await _context.Restoraunts.FirstOrDefaultAsync();
|
---|
84 | restaurant.Image = memoryStream.ToArray();
|
---|
85 | _context.Restoraunts.Update(restaurant);
|
---|
86 | _context.SaveChanges();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|