[057037b] | 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 | }
|
---|
| 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 | }
|
---|