public ResultModel CreateNews(NewsCreateRequestModel param) { ResultModel result = new ResultModel(); try { NewsBL bl = new NewsBL(); bl.CreateNews(1, param); result.Status = true; result.Message = "News created"; } catch (OrgException oex) { result.Status = false; result.Message = oex.Message; } catch (Exception ex) { result.Status = false; result.Message = AppConfigs.InternalErrorMessage; if (AppConfigs.DebugInternalMessage) { result.InternalMessage = ex.Message; } } return(result); }
public void CreateNews(int companyId, NewsCreateRequestModel model) { if ((model == null) || String.IsNullOrWhiteSpace(model.Text)) { throw new OrgException("Invalid note"); } using (OrgCommEntities dbc = new OrgCommEntities(DBConfigs.OrgCommConnectionString)) { if (!dbc.Company.Any(r => r.Id.Equals(companyId))) { throw new OrgException("Invalid company Id"); } OrgComm.Data.Models.News news = new OrgComm.Data.Models.News { CompanyId = companyId, CreatedBy = 1, //TEMP CreatedDate = DateTime.Now, LikeCount = 0, Likes = null, CommentCount = 0, Comments = null, Contents = new List <OrgComm.Data.Models.NewsContent>() }; if (!String.IsNullOrWhiteSpace(model.Text)) { byte[] data = null; //Convert any encoding to UTF8 if (System.Text.Encoding.Default is System.Text.UTF8Encoding) { data = System.Text.Encoding.Default.GetBytes(model.Text); } else { byte[] raw = System.Text.Encoding.Default.GetBytes(model.Text); data = System.Text.Encoding.Convert(Encoding.Default, Encoding.UTF8, raw); } news.Contents.Add(new OrgComm.Data.Models.NewsContent { Data = data, Type = (int)OrgComm.Data.Models.NewsContent.ContentType.Text }); } //Temporarily #region Add image type if (model.Image1 != null) { news.Contents.Add(new OrgComm.Data.Models.NewsContent { Data = model.Image1.Buffer, Type = (int)OrgComm.Data.Models.NewsContent.ContentType.Image }); } if (model.Image2 != null) { news.Contents.Add(new OrgComm.Data.Models.NewsContent { Data = model.Image2.Buffer, Type = (int)OrgComm.Data.Models.NewsContent.ContentType.Image }); } if (model.Image3 != null) { news.Contents.Add(new OrgComm.Data.Models.NewsContent { Data = model.Image3.Buffer, Type = (int)OrgComm.Data.Models.NewsContent.ContentType.Image }); } if (model.Image4 != null) { news.Contents.Add(new OrgComm.Data.Models.NewsContent { Data = model.Image4.Buffer, Type = (int)OrgComm.Data.Models.NewsContent.ContentType.Image }); } if (model.Image5 != null) { news.Contents.Add(new OrgComm.Data.Models.NewsContent { Data = model.Image5.Buffer, Type = (int)OrgComm.Data.Models.NewsContent.ContentType.Image }); } #endregion //-------end temporarily dbc.News.Add(news); dbc.SaveChanges(); } }