public IHttpActionResult Post(GenreViewModel genre) { var loggedUserId = HttpContext.Current.GetOwinContext().GetUserId(); genre.UserCreated = new Models.Users.UserViewModel() { Id = loggedUserId }; genre.Datecreated = DateTimeOffset.Now; var request = new SaveGenreRequest() { RequestToken = Guid.NewGuid(), UserId = loggedUserId, Genre = genre.MapToView() }; var genresResponse = _genreService.SaveGenre(request); if (!genresResponse.Success) { return(BadRequest(genresResponse.Message)); } return(Ok(genre = genresResponse.Genre.MapToViewModel())); }
/// <summary> /// Validation function /// </summary> /// <param name="item">SaveGenreRequest</param> /// <returns>System.Boolean: true if validation is ok, else false</returns> bool ServerValidation(SaveGenreRequest item) { try { if (item.Genre.Name == null || !(item.Genre.Name is String) || item.Genre.Name.Length > 50) { throw new ValidationException("Genre name is required or is bigger than 50!"); } // TODO fix server-validation for not required fields genre return(true); } catch (ValidationException e) { return(false); } }
/// <summary> /// Handling save request and response /// </summary> /// <param name="request">Messaging.Genres.SaveGenreRequest</param> /// <returns>Messaging.Genres.SaveGenreResponse</returns> SaveGenreResponse IGenreService.SaveGenre(SaveGenreRequest request) { var response = new SaveGenreResponse() { Request = request, ResponseToken = Guid.NewGuid() }; try { if (request.Genre?.Id == 0) { if (ServerValidation(request)) //server-side validation { response.Genre = request.Genre; response.Genre.Id = _repository.Add(request.Genre.MapToModel()); response.Success = true; } else { response.Success = false; } } else if (request.Genre?.Id > 0) { if (ServerValidation(request)) //server-side validation { response.Genre = _repository.Save(request.Genre.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); }