Esempio n. 1
0
        public async Task <ActionResult <Travel> > CreateEmployeeTravel([FromBody] CreateEmployeeTravelRequest request)
        {
            try
            {
                if (!await _employeeTravelRepository.CheckTravelsByEmployeeId(request.EmployeeId, request.TravelId))
                {
                    return(Conflict(" This employee already has this trip "));
                }

                var employeeTravel = new EmployeeTravel
                {
                    Employee = await _employeeRepository.FindById(request.EmployeeId),
                    Travel   = await _travelRepository.FindByIdWithOrganizedBy(request.TravelId),
                    Confirm  = request.Confirm,
                };
                if (request.NeedApartment)
                {
                    var _employeeTravel = await _employeeTravelRepository.Create(employeeTravel);

                    var _office = await _officeRepository.FindById(_employeeTravel.Travel.TravelTo.Id);

                    await _apartmentRepository.AddGuest(await _apartmentRepository.FindById(_office.OfficeApartment.Id),
                                                        _employeeTravel);

                    return(Ok());
                }
                else
                {
                    return(Ok(await _employeeTravelRepository.Create(employeeTravel)));
                }
            }
            catch (ArgumentException e)
            {
                return(Conflict(e.Message));
            }
        }