public IActionResult CreateCategory([FromBody] CategoryCreateDto categoryDto) { if (categoryDto == null) { return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } if (_repo.CategoryExists(categoryDto.Title)) { return(StatusCode(400, new { message = "Category with this title already exists" })); } if (!ModelState.IsValid) { return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } var categoryObj = _mapper.Map <Category>(categoryDto); categoryObj.CreationDateTime = DateTime.Now; if (!_repo.CreateCategory(categoryObj)) { return(StatusCode(500, new { message = $"Something went wrong when saving the record {categoryDto.Title}" })); } return(CreatedAtRoute("GetCategory", new { Version = HttpContext.GetRequestedApiVersion().ToString(), id = categoryObj.Id }, categoryObj)); }
public IActionResult UpdateOpinion(int id, [FromBody] OpinionUpdateDto opinionUpdateObj) { if (opinionUpdateObj == null || id != opinionUpdateObj.Id) { //return BadRequest(ModelState); return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } if (!ModelState.IsValid) { //return BadRequest(ModelState); return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } var opinionObj = _mapper.Map <Opinion>(opinionUpdateObj); opinionObj.CreationDateTime = DateTime.Now; if (!_repo.UpdateOpinion(opinionObj)) { //ModelState.AddModelError("", $"Error occurred during updating opinion with this content: {opinionObj.Content}"); return(StatusCode(500, new { message = $"Error occurred during updating opinion with this content: {opinionObj.Content}" })); } return(NoContent()); }
public IActionResult DeleteCategory(int id) { if (!_repo.CategoryExists(id)) { return(NotFound(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } var categoryObj = _repo.GetCategory(id); if (!_repo.DeleteCategory(categoryObj)) { //ModelState.AddModelError("", $"Error occurred during deleting object : {categoryObj.Title}"); return(StatusCode(500, new { message = $"Error occurred during deleting object : {categoryObj.Title}" })); } return(NoContent()); }
public IActionResult DeleteForumPost(int id) { if (!_repo.ForumPostIfExist(id)) { //return BadRequest(ModelState); return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } var forumObj = _repo.GetPost(id); if (!_repo.DeleteForumPost(forumObj)) { //ModelState.AddModelError("",$"Error occurred during deleting object with title: {forumObj.Title}"); return(StatusCode(500, new { message = $"Error occurred during deleting object with title: {forumObj.Title}" })); } return(NoContent()); }
public IActionResult UpdateForumPost(int id, [FromBody] ForumPostUpdateDto forumPostDto) { if (forumPostDto == null || id != forumPostDto.Id) { /*return BadRequest(ModelState);*/ return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } if (!_repo.CategoryIfExists(forumPostDto.CategoryId)) { return(BadRequest(new { message = "Category with this id isn't exist" })); } if (!ModelState.IsValid) { //return BadRequest(ModelState); return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } var obj = _repo.GetPost(forumPostDto.Title); if (obj != null) // Sprawdzanie czy tytuł po zmianie dalej będzie unikalny { if (obj.Id != forumPostDto.Id) { //ModelState.AddModelError("", "Category with this title already exists"); return(BadRequest(new { message = "Category with this title already exists" })); } } var forumPost = _mapper.Map <ForumPost>(forumPostDto); forumPost.Date = DateTime.Now; if (!_repo.UpdateForumPost(forumPost)) { //ModelState.AddModelError("", $"Error occurred during updating post with title: {forumPost.Title}"); return(StatusCode(500, new { message = $"Error occurred during updating post with title: {forumPost.Title}" })); } return(NoContent()); }
public IActionResult CreateOpinion([FromBody] OpinionCreateDto opinionCreateObj) { if (opinionCreateObj == null) { return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } if (!ModelState.IsValid) { return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } var opinion = _mapper.Map <Opinion>(opinionCreateObj); opinion.CreationDateTime = DateTime.Now; if (!_repo.CreateOpinion(opinion)) { return(StatusCode(500, new { message = $"Error occurred during creating opinion with content : {opinion.Content}" })); } return(CreatedAtRoute("GetOpinion", new { Version = HttpContext.GetRequestedApiVersion().ToString(), id = opinion.Id }, opinion)); }
public IActionResult UpdateCategory(int id, [FromBody] CategoryUpdateDto categoryUpdateDto) { if (categoryUpdateDto == null || categoryUpdateDto.Id != id) { return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); return(BadRequest(ModelState)); } if (!ModelState.IsValid) { return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); //return BadRequest(ModelState); } var obj = _repo.GetCategory(categoryUpdateDto.Title); if (obj != null) // Sprawdzanie czy tytuł po zmianie dalej będzie unikalny { if (obj.Id != categoryUpdateDto.Id) { //ModelState.AddModelError("", "Category with this title already exists"); return(BadRequest(new { message = "Category with this title already exists" })); } } var categoryObj = _mapper.Map <Category>(categoryUpdateDto); if (!_repo.UpdateCategory(categoryObj)) { //ModelState.AddModelError("", $"Error occured during updating object: {categoryUpdateDto.Title}"); return(StatusCode(500, new { message = $"Error occured during updating object: {categoryUpdateDto.Title}" })); } return(NoContent()); }
public IActionResult CreateForumPost([FromBody] ForumPostCreateDto forumPostDto) { if (forumPostDto == null) { //return BadRequest(ModelState); return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); } if (_repo.ForumPostIfExist(forumPostDto.Title)) { //ModelState.AddModelError("","Post with this title already exists"); return(NotFound(new { message = "Post with this title already exists" })); } if (!_repo.CategoryIfExists(forumPostDto.CategoryId)) { return(BadRequest(new { message = "Category with this id isn't exist" })); } if (!ModelState.IsValid) { return(BadRequest(new { message = ModelStateToString.ConvertModelStateToString(ModelState) })); //return BadRequest(ModelState); } var forumPost = _mapper.Map <ForumPost>(forumPostDto); forumPost.Date = DateTime.Now; if (!_repo.CreateForumPost(forumPost)) { //ModelState.AddModelError("",$"Error occurred during saving object with title: {forumPost.Title}"); return(StatusCode(500, new { message = $"Error occurred during saving object with title: {forumPost.Title}" })); } return(CreatedAtRoute("GetForumPost", new { Version = HttpContext.GetRequestedApiVersion().ToString(), id = forumPost.Id }, forumPost)); }