Exemplo n.º 1
0
        public async Task <IActionResult> Create(AppointmentTypeDto appointmentTypeDto)
        {
            var appointmentType  = _mapper.Map <AppointmentType>(appointmentTypeDto);
            var validationResult = _validator.Validate(appointmentType);

            if (!ModelState.IsValid)
            {
                var errors = ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage).ToList();
                return(BadRequest(errors));
            }
            else if (!validationResult.IsValid)
            {
                var validationErrors = validationResult.Errors.Select(x => $"{x.PropertyName} failed validation: ${x.ErrorMessage}.");
                return(BadRequest(string.Join(";", validationErrors)));
            }

            if (await _appointmentTypeRepository.AnyByName(appointmentType.Name).ConfigureAwait(false))
            {
                return(BadRequest("This appointment type already exists."));
            }

            await _appointmentTypeRepository.Create(appointmentType).ConfigureAwait(false);

            return(Ok("Appointment type created."));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <AppointmentTypeDto> > Put(int id, AppointmentTypeDto model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Model is not valid"));
                }

                var oldAppointmentType = await _appointmentTypeService.GetAppointmentTypeByIdAsync(id);

                if (oldAppointmentType == null)
                {
                    return(NotFound($"Could not find appointment type with id of {id}"));
                }

                var updatedAppointmentType = _mapper.Map(model, oldAppointmentType);

                if (await _appointmentTypeService.UpdateAppointmentType(updatedAppointmentType))
                {
                    return(Ok(updatedAppointmentType));
                }
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Database Failure: {e}"));
            }

            return(BadRequest());
        }
Exemplo n.º 3
0
        public async Task <ActionResult <AppointmentTypeDto> > Post(AppointmentTypeDto model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var existing = await _appointmentTypeService.GetAppointmentTypeByNameAsync(model.Name);

                    if (existing != null)
                    {
                        return(BadRequest("The appointment type is already exists!"));
                    }

                    var location = _linkGenerator.GetPathByAction(HttpContext,
                                                                  "Get", "AppointmentTypes",
                                                                  new { name = model.Name });

                    if (string.IsNullOrWhiteSpace(location))
                    {
                        return(BadRequest("Could not use current name"));
                    }

                    var appointmentType = _mapper.Map <AppointmentType>(model);

                    if (await _appointmentTypeService.CreateAppointmentType(appointmentType))
                    {
                        return(Created(location, appointmentType));
                    }

                    return(BadRequest("Something bad happened!"));
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Database Failure: {e}"));
            }
        }