public async Task <SystemResponse> RemovePost(long postId) { Post postObject = _dataContext.Set <Post>() .AsTracking() .FirstOrDefault(p => p.Id == postId); try { if (postObject == null) { throw new RecordNotFoundException($"Post is not found {postId}"); } _dataContext.Entry(postObject).State = EntityState.Deleted; await _dataContext.SaveChangesAsync(); return(SystemResponse.SuccessResponse("Success")); } catch (DbUpdateException ex) { _logger.LogError(ex, "Update post failed"); _dataContext.Entry(postObject).Reload(); throw new UpdateFailedException("Update post failed"); } catch (Exception ex) { _logger.LogError(ex, "Update post failed"); throw new UpdateFailedException("Update post failed"); } }
public async Task <SystemResponse> UpdatePost(Post updatePost) { Post postObject = _dataContext.Set <Post>() .AsTracking() .FirstOrDefault(p => p.Id == updatePost.Id); try { if (postObject == null) { throw new RecordNotFoundException($"Post is not found {updatePost.Id}"); } _dataContext.Entry(postObject).CurrentValues.SetValues(updatePost); await _dataContext.SaveChangesAsync(); return(SystemResponse.SuccessResponse("Success")); } catch (DbUpdateException ex) { _logger.LogError(ex, "Update post failed"); _dataContext.Entry(postObject).Reload(); throw new UpdateFailedException("Update post failed"); } catch (Exception ex) { _logger.LogError(ex, "Update post failed"); throw new UpdateFailedException("Update post failed"); } }