source: resTools_backend/backend/Services/RestaurantService.cs@ 63f5ced

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

reviews pagination

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[057037b]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();
[cc4db18]12 public Task UploadImage(IFormFile file);
13 public Task UpdateRestaurant(UpdateRestaurantRequest req);
[057037b]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
[899b19d]34 .Include(x => x.Menu)
35 .Include(x => x.Reviews).ThenInclude(x => x.User)
[057037b]36 .Select(x => new RestaurantResponse()
37 {
38 Name = x.Name,
[cc4db18]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
[899b19d]48 }).ToList(),
[63f5ced]49 Reviews = x.Reviews.Select(x => new ReviewResponse()
[899b19d]50 {
[63f5ced]51 CreatedAt = x.CreatedAt,
[899b19d]52 Description = x.Description,
[63f5ced]53 Id = x.Id,
[899b19d]54 Stars = x.Stars,
[63f5ced]55 Title = x.Title,
[899b19d]56 Username = x.User == null ? "Anonymous" : x.User.Email
57 }).ToList(),
58 AverageReview = x.Reviews.Count>0 ? x.Reviews.Select(x => x.Stars).Average() : 0
[057037b]59 })
60 .FirstOrDefaultAsync();
61 return res;
62 }
[cc4db18]63
64 public async Task UpdateRestaurant(UpdateRestaurantRequest req)
65 {
66 var restaurant = await _context.Restoraunts.FirstOrDefaultAsync();
67 restaurant.Name = req.Name;
68 restaurant.Address = req.Address;
69 restaurant.Phone = req.Phone;
70 _context.Restoraunts.Update(restaurant);
71 await _context.SaveChangesAsync();
72 }
73
74 public async Task UploadImage(IFormFile file)
75 {
76 using (var memoryStream = new MemoryStream())
77 {
78 await file.CopyToAsync(memoryStream);
79 var restaurant = await _context.Restoraunts.FirstOrDefaultAsync();
80 restaurant.Image = memoryStream.ToArray();
81 _context.Restoraunts.Update(restaurant);
82 _context.SaveChanges();
83 }
84 }
[057037b]85 }
86}
Note: See TracBrowser for help on using the repository browser.