public OperationResult Add(BlogDto dto) { try { var validate = Validate(dto); if (validate != null) return validate; Blog blog = dto.MapTo<Blog>(); blog.createTime = DateTime.Now; BlogRepository.Insert(blog); return new OperationResult(OperationResultType.Success, "添加博客成功!"); } catch (Exception ex) { var msg = ex.Message; //log 记录异常信息 //返回用户友好的提示 return new OperationResult(OperationResultType.Error, "添加博客失败!"); } }
public BlogDto GetByKey(int id) { var blog = this.Entities.FirstOrDefault(w => w.Id == id); if (blog != null) { var dto = new BlogDto() { id = blog.Id, title = blog.title }; return dto; } return null; }
private OperationResult Validate(BlogDto dto) { if (dto.title.IsNullOrEmpty() || dto.title.Trim().IsNullOrEmpty()) { return new OperationResult(OperationResultType.ValidError, "博客标题不能为空!"); } return null; }
public OperationResult Update(BlogDto dto) { try { var validate = Validate(dto); if (validate != null) return validate; Blog blog = BlogRepository.GetById(dto.id); if (blog == null) { return new OperationResult(OperationResultType.QueryNull); } blog = dto.MapTo(blog); BlogRepository.Update(blog); return new OperationResult(OperationResultType.Success, "更新博客成功!"); } catch (Exception ex) { var msg = ex.Message; //log 记录异常信息 //返回用户友好的提示 return new OperationResult(OperationResultType.Error, "更新博客失败!"); } }