public async Task <ActionResult <ActorModel> > Put(string firstName, string lastName, ActorModel model) { try { var filmsModelValidationResult = ModelsValidator.ValidateFilmModels(model); if (!filmsModelValidationResult.Item1) { return(BadRequest(filmsModelValidationResult.Item2)); } var actor = await _repository.GetActorByNameAsync(firstName, lastName); if (actor == null) { return(BadRequest($"Actor {firstName} {lastName} doesn't exist")); } _mapper.Map(model, actor); var updateFilmsResult = await UpdateActorsFilms(actor); if (!updateFilmsResult.Item1) { return(BadRequest(updateFilmsResult.Item2)); } if (await _repository.SaveChangesAsync()) { actor = await _repository.GetActorByNameAsync(firstName, lastName, true); return(_mapper.Map <ActorModel>(actor)); } return(StatusCode(StatusCodes.Status500InternalServerError, "It looks like no changes were made")); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure")); } }
public async Task <ActionResult <DirectorModel> > Put(string firstName, string lastName, DirectorModel model) { try { var filmsModelValidationResult = ModelsValidator.ValidateFilmModels(model); if (!filmsModelValidationResult.Item1) { return(BadRequest(filmsModelValidationResult.Item2)); } var director = await _repository.GetDirectorByNameAsync(firstName, lastName); if (director == null) { return(BadRequest("There is director with the name in db")); } _mapper.Map(model, director); var filmsUpdateResult = await UpdateFilmForDirector(director); if (!filmsUpdateResult.Item1) { return(BadRequest(filmsUpdateResult.Item2)); } if (await _repository.SaveChangesAsync()) { return(Created($"api/Directors/{model.FirstName}/{model.LastName}", _mapper.Map <DirectorModel>(director))); } return(StatusCode(StatusCodes.Status500InternalServerError, "It looks like no changes were made")); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure")); } }
public async Task <ActionResult <DirectorModel> > Post(DirectorModel model) { try { if (model.Films != null && model.Films.Count() > 0) { return(BadRequest("Please add director first. Than you can update list of films with PUT request")); } var filmsModelValidationResult = ModelsValidator.ValidateFilmModels(model); if (!filmsModelValidationResult.Item1) { return(BadRequest(filmsModelValidationResult.Item2)); } var existingDirector = await _repository.GetDirectorByNameAsync(model.FirstName, model.LastName); if (existingDirector != null) { return(BadRequest("There is director with the name in db")); } var director = _mapper.Map <Director>(model); _repository.Add(director); if (await _repository.SaveChangesAsync()) { return(Created($"api/Directors/{model.FirstName}/{model.LastName}", _mapper.Map <DirectorModel>(director))); } return(StatusCode(StatusCodes.Status500InternalServerError, "It looks like no changes were made")); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database failure")); } }