source: resTools_backend/backend/Services/RestaurantService.cs@ 7a983b0

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

move files

  • 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 IRestaurantService
9 {
10 public Task CreateRestaurant(string name, int userId);
11 public Task<RestaurantResponse> GetRestaurant();
12 }
13 public class RestaurantService : IRestaurantService
14 {
15 private readonly DataContext _context = null;
16
17 public RestaurantService(DataContext context)
18 {
19 _context = context;
20 }
21 public async Task CreateRestaurant(string name, int userId)
22 {
23 User user = await _context.Users.FindAsync(userId);
24 Restaurant restaurant = new Restaurant() { Name = name, Owner = user};
25 await _context.Restoraunts.AddAsync(restaurant);
26 await _context.SaveChangesAsync();
27 }
28
29 public async Task<RestaurantResponse> GetRestaurant()
30 {
31 RestaurantResponse res = await _context.Restoraunts
32 .Include(x => x.Reservations)
33 .Select(x => new RestaurantResponse()
34 {
35 Id = x.Id,
36 Name = x.Name,
37 OwnerId = x.OwnerFk,
38 Reservations = x.Reservations.Select(t => new ReservationResponse()
39 {
40 ContactName = t.ContactName,
41 ContactNumber = t.ContactNumber,
42 Persons = t.Persons,
43 StartDate = t.StartDate,
44 ReservationStatus = t.ReservationStatus,
45 Id = t.Id,
46 ReservationPlace = t.ReservationPlace,
47 ReservationType = t.ReservationType
48 }).ToList()
49 })
50 .FirstOrDefaultAsync();
51 return res;
52 }
53 }
54}
Note: See TracBrowser for help on using the repository browser.