public IHttpActionResult Put(int id, MoviePersonViewModel moviePerson) { var loggedUserId = HttpContext.Current.GetOwinContext().GetUserId(); moviePerson.UserLastModified = new Models.Users.UserViewModel() { Id = loggedUserId }; moviePerson.Lastmodified = DateTimeOffset.Now; if (ModelState.IsValid) { var request = new SaveMoviePersonRequest() { RequestToken = Guid.NewGuid(), UserId = loggedUserId, MoviePerson = moviePerson.MapToView() }; var moviePersonsResponse = _moviePersonService.SaveMoviePerson(request); if (!moviePersonsResponse.Success) { return(BadRequest(moviePersonsResponse.Message)); } return(Ok(moviePerson = moviePersonsResponse.MoviePerson.MapToViewModel())); } else { return(BadRequest()); } }
/// <summary> /// Handling save request and response /// </summary> /// <param name="request">Messaging.MoviePersons.SaveMoviePersonRequest</param> /// <returns>Messaging.MoviePersons.SaveMoviePersonResponse</returns> public SaveMoviePersonResponse SaveMoviePerson(SaveMoviePersonRequest request) { var response = new SaveMoviePersonResponse() { Request = request, ResponseToken = Guid.NewGuid() }; try { if (request.MoviePerson?.Id == 0) { if (ServerValidation(request)) //server-side validation { response.MoviePerson = request.MoviePerson; response.MoviePerson.Id = _repository.Add(request.MoviePerson.MapToModel()); response.Success = true; } else { response.Success = false; } } else if (request.MoviePerson?.Id > 0) { if (ServerValidation(request)) //server-side validation { response.MoviePerson = _repository.Save(request.MoviePerson.MapToModel()).MapToView(); response.Success = true; } else { response.Success = false; } } else { response.Success = false; } } catch (Exception ex) { response.Message = ex.Message; response.Success = false; } return(response); }
/// <summary> /// Validation function /// </summary> /// <param name="item">SaveMoviePersonRequest</param> /// <returns>System.Boolean: true if validation is ok, else false</returns> bool ServerValidation(SaveMoviePersonRequest item) { try { if (item.MoviePerson.FirstName == null || !(item.MoviePerson.LastName is String) || item.MoviePerson.FirstName.Length > 50) { throw new ValidationException("First name is required or is bigger than 50!"); } if (item.MoviePerson.LastName == null || !(item.MoviePerson.LastName is String) || item.MoviePerson.LastName.Length > 50) { throw new ValidationException("Last name name is required or is bigger than 50!"); } if (item.MoviePerson.Birthday == null) { throw new ValidationException("Birthday is required!"); } if (item.MoviePerson.BirthPlace == null || !(item.MoviePerson.BirthPlace is String) || item.MoviePerson.BirthPlace.Length > 50) { throw new ValidationException("Birthplace is required!"); } if (item.MoviePerson.Popularity < 1 || item.MoviePerson.Popularity > 100) { throw new ValidationException("Popularity is required or is not inside required rang(1,100)"); } // TODO fix server-validation for not required fields //if (item.MoviePerson.Biography is String && item.MoviePerson.Biography.Length < 2000) throw new ValidationException("Biography is not string or bigger than 2000 characters!"); //if (item.MoviePerson.IMDBUrl is String && item.MoviePerson.IMDBUrl.Length < 2000) throw new ValidationException("IMDBUrl is not string or bigger than 2000 characters!"); //if (item.MoviePerson.PhotoUrl is String && item.MoviePerson.PhotoUrl.Length < 2000) throw new ValidationException("PhotoUrl is not string or bigger than 2000 characters!"); return(true); } catch (ValidationException e) { return(false); } }