public async Task GetAccessByHotelStaff(int userId, AccessByWorkerDTO workerDTO) { var room = context.Rooms.Get(workerDTO.RoomId); if (room == null) { throw new NotFoundException("No such room"); } bool hasPermission = false; HotelStaff staff = context.HotelStaffs.Get(userId); room = context.Rooms.Get(workerDTO.RoomId); hasPermission = room.HotelId == staff.Hotel.Id; if (!hasPermission) { throw new PermissionException(); } var lastAccess = room.RoomAccesses.Where(r => r.UserId == userId).OrderBy(x => x.DateTime).LastOrDefault(); bool isOpened = (lastAccess == null) ? true : !lastAccess.IsOpen; var access = new RoomAccess() { DateTime = DateTime.Now, IsOpen = isOpened, RoomId = workerDTO.RoomId, UserId = userId, ReasonOfVisit = workerDTO.Reason }; context.RoomAccesses.Create(access); await context.SaveAsync(); }
public async Task GetAccessByHotelAdmin([FromBody] AccessByWorkerDTO accessByWorkerDTO) { await accessService.GetAccessByHotelStaff(User.ConvertToUserData().Id, accessByWorkerDTO); }