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 IReservationService
|
---|
9 | {
|
---|
10 | public Task CreateReservation(CreateReservationRequest req);
|
---|
11 | public Task ChangeReservationStatus(int resId, ReservationStatus status);
|
---|
12 | public Task<List<ReservationResponse>> GetReservatins(DateTime from, DateTime to);
|
---|
13 | public Task<List<ReservationResponse>> GetNewReservations();
|
---|
14 | public Task AssignTable(int tableId, int reservationId);
|
---|
15 | }
|
---|
16 | public class ReservationService : IReservationService
|
---|
17 | {
|
---|
18 | private readonly DataContext _context = null;
|
---|
19 | private readonly ISmsService _smsService = null;
|
---|
20 |
|
---|
21 | public ReservationService(DataContext context, ISmsService smsService)
|
---|
22 | {
|
---|
23 | _context = context;
|
---|
24 | _smsService = smsService;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public async Task AssignTable(int tableId, int reservationId)
|
---|
28 | {
|
---|
29 | var reservation = await _context.Reservations.FindAsync(reservationId);
|
---|
30 | reservation.Table = tableId;
|
---|
31 | _context.Update(reservation);
|
---|
32 | await _context.SaveChangesAsync();
|
---|
33 | }
|
---|
34 |
|
---|
35 | public async Task ChangeReservationStatus(int resId, ReservationStatus status)
|
---|
36 | {
|
---|
37 | var reservation = await _context.Reservations.FindAsync(resId);
|
---|
38 | reservation.ReservationStatus = status;
|
---|
39 | _context.Update(reservation);
|
---|
40 | await _context.SaveChangesAsync();
|
---|
41 | _smsService.ReservationStatusUpdate(reservation.ContactName, reservation.ContactNumber, reservation.ReservationStatus);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public async Task CreateReservation(CreateReservationRequest req)
|
---|
45 | {
|
---|
46 | Restaurant res = await _context.Restoraunts.Include(x => x.Reservations).FirstOrDefaultAsync();
|
---|
47 | Reservation reservation = new Reservation()
|
---|
48 | {
|
---|
49 | ContactName = req.ContactName,
|
---|
50 | ContactNumber = req.ContactNumber,
|
---|
51 | ReservationPlace = req.ReservationPlace,
|
---|
52 | ReservationType = req.ReservationType,
|
---|
53 | StartDate = req.StartDate,
|
---|
54 | Persons = req.Persons,
|
---|
55 | ReservationStatus = ReservationStatus.New
|
---|
56 | };
|
---|
57 | res.Reservations.Add(reservation);
|
---|
58 | _context.Restoraunts.Update(res);
|
---|
59 | await _context.SaveChangesAsync();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public async Task<List<ReservationResponse>> GetNewReservations()
|
---|
63 | {
|
---|
64 | Restaurant res = await _context.Restoraunts
|
---|
65 | .Include(x => x.Reservations
|
---|
66 | .Where(x => x.ReservationStatus == ReservationStatus.New))
|
---|
67 | .FirstOrDefaultAsync();
|
---|
68 | var reservations = res.Reservations.Select(t => new ReservationResponse()
|
---|
69 | {
|
---|
70 | ContactName = t.ContactName,
|
---|
71 | ContactNumber = t.ContactNumber,
|
---|
72 | Persons = t.Persons,
|
---|
73 | StartDate = t.StartDate,
|
---|
74 | ReservationStatus = t.ReservationStatus,
|
---|
75 | Table = t.Table,
|
---|
76 | Id = t.Id,
|
---|
77 | ReservationPlace = t.ReservationPlace,
|
---|
78 | ReservationType = t.ReservationType
|
---|
79 | }).OrderByDescending(x => x.ReservationStatus == ReservationStatus.New).ToList();
|
---|
80 | return reservations;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public async Task<List<ReservationResponse>> GetReservatins(DateTime from, DateTime to)
|
---|
84 | {
|
---|
85 | Restaurant res = await _context.Restoraunts
|
---|
86 | .Include(x => x.Reservations
|
---|
87 | .Where(x => x.StartDate.CompareTo(from)>0 && x.StartDate.CompareTo(to)<=0))
|
---|
88 | .FirstOrDefaultAsync();
|
---|
89 | var reservations = res.Reservations.Select(t => new ReservationResponse()
|
---|
90 | {
|
---|
91 | ContactName = t.ContactName,
|
---|
92 | ContactNumber = t.ContactNumber,
|
---|
93 | Persons = t.Persons,
|
---|
94 | StartDate = t.StartDate,
|
---|
95 | ReservationStatus = t.ReservationStatus,
|
---|
96 | Table = t.Table,
|
---|
97 | Id = t.Id,
|
---|
98 | ReservationPlace = t.ReservationPlace,
|
---|
99 | ReservationType = t.ReservationType
|
---|
100 | }).OrderByDescending(x => x.ReservationStatus == ReservationStatus.New).ToList();
|
---|
101 | return reservations;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|