Beispiel #1
0
        public void CreateComment(int memberId, NewsCommentCreateRequesetModel model)
        {
            if ((model == null) || !model.NewsId.HasValue)
            {
                throw new OrgException("Invalid news");
            }

            if ((model == null) || !model.Type.HasValue)
            {
                throw new OrgException("Invalid comment type");
            }

            using (OrgCommEntities dbc = new OrgCommEntities(DBConfigs.OrgCommConnectionString))
            {
                OrgComm.Data.Models.News news = dbc.News.SingleOrDefault(r => r.Id == model.NewsId.Value);

                if (news == null)
                {
                    throw new OrgException("News not found");
                }

                OrgComm.Data.Models.NewsComment comment = new OrgComm.Data.Models.NewsComment
                {
                    Text        = model.Text,
                    Type        = model.Type.Value,
                    MemberId    = memberId,
                    CreatedDate = DateTime.Now
                };

                news.Comments.Add(comment);
                news.CommentCount = news.Comments.Count();

                dbc.SaveChanges();
            }
        }
Beispiel #2
0
        public IList <NewsCommentModel> GetCommentsByNewsId(NewsCommentByCriteriaRequestModel model)
        {
            if ((model == null) || !model.NewsId.HasValue)
            {
                throw new OrgException("Invalid news");
            }

            using (OrgCommEntities dbc = new OrgCommEntities(DBConfigs.OrgCommConnectionString))
            {
                OrgComm.Data.Models.News news = dbc.News.SingleOrDefault(r => r.Id == model.NewsId.Value);

                if (news == null)
                {
                    throw new OrgException("News not found");
                }

                var qry = (from c in news.Comments
                           join m in dbc.Members on c.MemberId equals m.Id
                           select new OrgComm.Data.Models.NewsComment
                {
                    Id = c.Id,
                    MemberId = (m.DelFlag)? 0 : c.MemberId,
                    NewsId = c.NewsId,
                    Text = c.Text,
                    Type = c.Type,
                    CreatedDate = c.CreatedDate
                }
                           );

                qry = qry.OrderByDescending(r => r.CreatedDate);

                if (model != null)
                {
                    if (model.PageSize.HasValue && (model.PageSize.Value > 0))
                    {
                        if (model.Page.HasValue && (model.Page.Value > 1))
                        {
                            qry = qry.Skip((model.Page.Value - 1) * model.PageSize.Value);
                        }

                        qry = qry.Take(model.PageSize.Value);
                    }
                }

                return(qry.Select(r => new NewsCommentModel
                {
                    Id = r.Id,
                    MemberId = r.MemberId,
                    Text = r.Text,
                    Type = r.Type,
                    CreatedDate = r.CreatedDate.ToString(AppConfigs.GeneralDateTimeFormat),
                    Ticks = r.CreatedDate.Ticks
                }).ToList());
            }
        }
Beispiel #3
0
        public void SetLikeForNews(int memberId, NewsLikeRequesetModel model)
        {
            if ((model == null) || !model.NewsId.HasValue)
            {
                throw new OrgException("Invalid news");
            }

            using (OrgCommEntities dbc = new OrgCommEntities(DBConfigs.OrgCommConnectionString))
            {
                OrgComm.Data.Models.News news = dbc.News.SingleOrDefault(r => r.Id == model.NewsId.Value);

                if (news == null)
                {
                    throw new OrgException("News not found");
                }

                var like = news.Likes.SingleOrDefault(r => r.MemberId == memberId);

                if (model.Type.HasValue)
                {
                    if (like == null)
                    {
                        news.Likes.Add(new OrgComm.Data.Models.NewsLike {
                            MemberId = memberId, LikeType = model.Type.Value, CreatedDate = DateTime.Now
                        });
                    }
                    else
                    {
                        like.LikeType = model.Type.Value;
                    }
                }
                else
                {
                    if (like != null)
                    {
                        dbc.Entry(like).State = System.Data.Entity.EntityState.Deleted;
                    }
                }

                news.LikeCount = news.Likes.Count();

                dbc.SaveChanges();
            }
        }
Beispiel #4
0
        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();
            }
        }