/// <summary> /// 验证一篇said是否是有效的said,同时矫正Said的数据 /// </summary> /// <param name="model">要验证的model</param> /// <returns>返回null表示验证成功,否则返回验证失败的字符串,用,号分割</returns> public string ValidateAndCorrectSubmit(Article model, SongApplication songApplication, ImageApplication imageApplication) { StringBuilder str = new StringBuilder(); //防止tag有HTML标签,修正 if (!string.IsNullOrWhiteSpace(model.STag)) { model.STag = HTMLCommon.HTMLTrim(model.STag); } //model.Song = SongApplication.Context.GetById(model.SongId);//检索有没有歌曲信息 if (songApplication.FindById(model.SongId) == null) { str.Append("歌曲信息不正确(不可获取),"); } if (imageApplication.FindById(model.ImageId) == null) { str.Append("缩略图信息不正确(不可获取),"); } foreach (var validateResult in model.Validate()) { //validateResult.MemberNames//这个要搞懂怎么用,或许能让提示信息更全一点 str.Append(validateResult.ErrorMessage + ","); } if (str.Length > 0) { str.Length--;//StringBuilder的length可以用于裁剪字符串? } else { if (string.IsNullOrEmpty(model.SaidId)) { //新增 if (string.IsNullOrWhiteSpace(model.SName) || FindByFileName(model.SName.Trim()) != null) //没有文件名或文件名不合法,则生成一个新的文件名 { model.SName = FileCommon.CreateFileNameByTime(); } } else { //编辑 if (string.IsNullOrWhiteSpace(model.SName)) { model.SName = FileCommon.CreateFileNameByTime(); } else { //从数据库中检索是否存在 var SaidIdLists = FindSaidIdByFileName(model.SName).ToList(); //文件名重复 if (SaidIdLists.Count > 1 && SaidIdLists.IndexOf(model.SaidId) > -1) { model.SName = FileCommon.CreateFileNameByTime(); } } } } return(str.Length > 0 ? str.ToString() : null); }
/// <summary> /// 验证音乐 /// </summary> /// <param name="model"></param> /// <returns></returns> public string ValidateAndCorrectSubmit(Song model, ImageApplication imageApplication) { if (string.IsNullOrWhiteSpace(model.SongName)) { return("歌曲名称不可为空(不可获取)"); } if (string.IsNullOrWhiteSpace(model.ImageId)) { return("歌曲图片不可为空(不可获取)"); } if (string.IsNullOrWhiteSpace(model.SongArtist)) { return("歌手不可为空(不可获取)"); } if (string.IsNullOrWhiteSpace(model.SongFileName)) { return("歌曲文件名不可为空(不可获取)"); } if (model.Duration < 1) { return("歌曲时长不正确"); } if (model.SongSize < 1) { return("歌曲大小不正确"); } /* * Error:一个实体对象不能由多个 IEntityChangeTracker 实例引用。 * 不能给原Model赋值!!只能给ImageId属性赋值,如果给原Model赋值,因为不同的DbContext,导致EF认为这个Image是新增的,但其实是引用 * * 参考: * http://m.blog.csdn.net/blog/jerry_cool_2010/6922095 * http://www.tuicool.com/articles/zEjEJn * http://q.cnblogs.com/q/43962/ */ //model.Image = ImageApplication.Find(model.ImageId); Image image = imageApplication.FindById(model.ImageId); if (image == null) { return("歌曲不正确(不可获取)"); } else { //更新引用 image.ReferenceCount += 1; imageApplication.Update(image); if (!imageApplication.Commit()) { return("删除歌曲图片异常"); } } return(null); }
/// <summary> /// 验证和修正新增的Banner对象 /// </summary> /// <param name="model"></param> /// <returns></returns> public string ValidateAndCorrectSubmit(Banner model, ImageApplication imageApplication) { if (model == null) { return("服务器接收的数据不正确"); } if (string.IsNullOrWhiteSpace(model.Link)) { return("Banner链接不允许为空"); } if (string.IsNullOrWhiteSpace(model.SourceCode) || string.IsNullOrWhiteSpace(model.HTML)) { return("Banner文本源码不允许为空"); } if (string.IsNullOrWhiteSpace(model.ImageId)) { return("Banner图片不正确"); } if (imageApplication.FindById(model.ImageId) == null) { return("没有找到正确的Banner图片"); } return(string.Empty); }