public long UpdatePostDetail(PostDetailCreateModel model) { try { var entity = _dbCMSContext.PostDetails.Find(model.Id); if (entity == null || entity.Deleted) { throw new ArgumentNullException("Post detail"); } int oldFileStreamId = entity.FileStreamFid; entity = _mapper.Map(model, entity); entity.UniqueId = entity.UniqueId ?? UniqueIDHelper.GenerateRandomString(UNIQUE_ID_LENGTH); entity.LastModifiedDate = DateTime.Now; entity.LastModifiedBy = _workContext.UserGuid; _dbCMSContext.SaveChanges(); if (entity.Id > 0) { if (model.FileStreamFid > 0 && oldFileStreamId != model.FileStreamFid) { #region Insert new post file stream entity.FileTypeFid = (int)FileTypeEnum.Image; var fileStreamModel = PreparingPostFileStreamCreateModel(entity, PostFileCategoryEnum.Thumbnail); _postFileStreamService.CreatePostFileStream(fileStreamModel); _postFileStreamService.DeletePostFileStream(oldFileStreamId); #endregion } if (model.FileDescriptionIds.Count > 0) { foreach (var id in model.FileDescriptionIds) { var item = PreparingPostFileStreamCreateModel(entity, PostFileCategoryEnum.Detail); item.FileStreamFid = id; _postFileStreamService.CreatePostFileStream(item); } } var havePostDetailActive = _dbCMSContext.PostDetails.Count(x => x.PostFid == entity.PostFid && x.IsActivated) > 0; var post = _dbCMSContext.Posts.Find(entity.PostFid); post.IsActivated = havePostDetailActive; post.LastModifiedBy = _workContext.UserGuid; post.LastModifiedDate = DateTime.Now; _dbCMSContext.SaveChanges(); } return(entity.Id); } catch (Exception ex) { AQException.ThrowException(ex); return(0); } }
public IActionResult DeletePost(int id) { var res = _postFileStreamService.DeletePostFileStream(id); return(OkBaseResponse(res)); }