public async Task <ActionResult <HairdresserModel> > Post(HairdresserModel model)
        {
            try
            {
                var hairdresser = _mapper.Map <Hairdresser>(model);

                if (model.SalonId == 0)
                {
                    return(BadRequest());
                }
                var salon = await _salonRepository.GetSalonByIdAsync(model.SalonId);

                if (salon == null)
                {
                    return(NotFound());
                }

                hairdresser.Salon = salon;

                _repository.Add(hairdresser);
                if (await _repository.SaveChangesAsync())
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed database"));
            }
        }
        public async Task <ActionResult <SalonModel> > GetById(int id)
        {
            try
            {
                var salon = await _repository.GetSalonByIdAsync(id);

                if (salon == null)
                {
                    return(NotFound());
                }

                return(_mapper.Map <SalonModel>(salon));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
        }
Beispiel #3
0
        public async Task <ActionResult <RegistrationModel> > Post(RegistrationModel model)
        {
            try
            {
                var exist = await _repository.GetRegistrationByDate_TimeAsync(model.Day, model.TimeOfDay);

                if (exist != null)
                {
                    return(BadRequest("The registration in use"));
                }

                var url = _linkGenerator.GetPathByAction("Get",
                                                         "Registrations",
                                                         new { Day = model.Day, TimeOfDay = model.TimeOfDay });

                if (string.IsNullOrWhiteSpace(url))
                {
                    return(BadRequest("Could not use current Day and Time"));
                }

                Registration registration = _mapper.Map <Registration>(model);
                if (model.ClientId != 0)
                {
                    var client = _clientsRespository.GetById(model.ClientId);
                    if (client == null)
                    {
                        return(NotFound());
                    }

                    registration.Client = client;
                }
                if (model.HairdresserId != 0)
                {
                    var dresser = _hairdresserRepository.GetHairdresserById(model.HairdresserId);
                    if (dresser == null)
                    {
                        return(NotFound());
                    }
                    registration.Hairdresser = dresser;
                }
                if (model.SalonId != 0)
                {
                    var salon = await _salonRepository.GetSalonByIdAsync(model.SalonId);

                    if (salon == null)
                    {
                        return(NotFound());
                    }
                    registration.Salon = salon;
                }

                _repository.Add(registration);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created(url, _mapper.Map <RegistrationModel>(registration)));
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed database"));
            }
        }