Exemplo n.º 1
0
        public async Task <IActionResult> SaveAnsAsync([FromBody] Answers Ans)
        {
            _bContext     = new BloggingContext();
            Ans.AnswredOn = DateTime.Now;
            var Rslt = await GetCrntUser(HttpContext);

            Ans.AnswredBy = Rslt.UsId;
            _bContext.Answers.Add(Ans);
            await _bContext.SaveChangesAsync();

            DParameter db_pam = new DParameter();

            db_pam.Id = Ans.Qid.ToString();
            var qst = await _bContext.Questions.FirstOrDefaultAsync(x => x.Qid == Convert.ToInt32(Ans.Qid));

            Notification ntf = new Notification();

            ntf.NotFrom     = Rslt.UsId;
            ntf.NotDateTime = DateTime.Now;
            ntf.NotIsread   = false;
            ntf.NotRoute    = "/PartIss/" + Ans.Qid.ToString();
            ntf.NotBody     = "Your Question has been answered by " + Rslt.FirstName + Rslt.LastName + "";
            ntf.NotTo       = qst.AskedBy;
            var exsists = await _bContext.Notification.FirstOrDefaultAsync(x => x.NotFrom == ntf.NotFrom && x.NotTo == ntf.NotTo && x.NotRoute == ntf.NotRoute);

            if (exsists == null)
            {
                await _bContext.Notification.AddAsync(ntf);

                await _bContext.SaveChangesAsync();
            }
            else
            {
                exsists.NotDateTime            = DateTime.Now;
                _bContext.Entry(exsists).State = EntityState.Modified;
                await _bContext.SaveChangesAsync();
            }
            var msg = await(from noti in _bContext.Notification
                            join usr in _bContext.Users on noti.NotFrom equals usr.UsId
                            where noti.NotId == ntf.NotId
                            select new
            {
                fromid   = usr.FirstName + usr.LastName,
                prof     = Request.Scheme + "://" + Request.Host.Value + (usr.ImagePath == null ? Url.Content($"/Contents/Images/Defaultprof.png") : Url.Content($"/Contents/Images/{usr.ImagePath}")),
                body     = noti.NotBody,
                datetime = noti.NotDateTime,
                nav      = noti.NotRoute
            }).OrderByDescending(x => x.datetime).ToListAsync();
            var partusr = await GetPartUsr(Convert.ToInt32(Ans.Qid.ToString()));

            if (partusr.Count > 0)
            {
                foreach (var x in partusr)
                {
                    await Notification.Clients.Client(x.ConnectionId.ToString()).SendAsync("MessageReceived", msg[0]);
                }
            }
            return(GetPartQstAndAns(db_pam));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> SearchTags(DParameter Dp)
        {
            _bContext = new BloggingContext();
            var result = await(from Tag in _bContext.Tags
                               where Tag.Tagname.Contains(Dp.Name)
                               select new
            {
                id   = Tag.TgId,
                Name = Tag.Tagname
            }).Take(10).OrderBy(x => x.Name).ToListAsync();

            return(Ok(result));
        }
Exemplo n.º 3
0
        public IActionResult GetParticularusr([FromBody] DParameter Dp)
        {
            _bContext = new BloggingContext();
            var result = (from usr in _bContext.Users
                          where usr.UsId == Convert.ToInt32(Dp.Id)
                          select new
            {
                Fullname = usr.FirstName + "" + usr.LastName,
                DOB = usr.Dob,
                profile = Request.Scheme + "://" + Request.Host.Value + (usr.ImagePath == null ? Url.Content($"/Contents/Images/Defaultprof.png") : Url.Content($"/Contents/Images/{usr.ImagePath}")),
                contact = usr.EmailId,
                Questions = _bContext.Questions.Where(x => x.AskedBy == usr.UsId).Count(),
                Answers = _bContext.Answers.Where(x => x.AnswredBy == usr.UsId).Count()
            }).ToList();

            return(Ok(result));
        }
Exemplo n.º 4
0
        public async Task <bool> CheckUserAvailable(DParameter param)
        {
            try
            {
                _bContext = new BloggingContext();
                var usrs = await _bContext.Users.FirstOrDefaultAsync(x => x.EmailId == param.Id.ToString());

                if (usrs != null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(true);
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> LikeAns([FromBody] DParameter Dp)
        {
            try
            {
                _bContext = new BloggingContext();
                var Rslt = await GetCrntUser(HttpContext);

                if (Rslt != null)
                {
                    var lks = await _bContext.Likes.FirstOrDefaultAsync(x => x.AnsId == Convert.ToInt32(Dp.Id.ToString()) && x.Likeby == Rslt.UsId);

                    if (lks == null)
                    {
                        Likes likes = new Likes();
                        likes.AnsId  = Convert.ToInt32(Dp.Id.ToString());
                        likes.LkCnt  = 1;
                        likes.Likeby = Rslt.UsId;
                        await _bContext.Likes.AddAsync(likes);

                        await _bContext.SaveChangesAsync();

                        var Ans = await _bContext.Answers.FirstOrDefaultAsync(x => x.Aid == likes.AnsId);

                        var qst = await _bContext.Questions.FirstOrDefaultAsync(x => x.Qid == Ans.Qid);

                        Notification ntf = new Notification();
                        ntf.NotFrom     = Rslt.UsId;
                        ntf.NotDateTime = DateTime.Now;
                        ntf.NotIsread   = false;
                        ntf.NotRoute    = "/PartIss/" + qst.Qid.ToString();
                        ntf.NotBody     = "Your Answer Liked by " + Rslt.FirstName + Rslt.LastName + "";
                        ntf.NotTo       = Ans.AnswredBy;
                        await _bContext.Notification.AddAsync(ntf);

                        await _bContext.SaveChangesAsync();

                        var msg = await(from noti in _bContext.Notification
                                        join usr in _bContext.Users on noti.NotFrom equals usr.UsId
                                        where noti.NotId == ntf.NotId
                                        select new
                        {
                            fromid   = usr.FirstName + usr.LastName,
                            prof     = Request.Scheme + "://" + Request.Host.Value + (usr.ImagePath == null ? Url.Content($"/Contents/Images/Defaultprof.png") : Url.Content($"/Contents/Images/{usr.ImagePath}")),
                            body     = noti.NotBody,
                            datetime = noti.NotDateTime,
                            nav      = noti.NotRoute
                        }).OrderByDescending(x => x.datetime).ToListAsync();
                        var partusr = await GetPartUsrByAns(Convert.ToInt32(Ans.Aid.ToString()));

                        if (partusr.Count > 0)
                        {
                            foreach (var x in partusr)
                            {
                                await Notification.Clients.Client(x.ConnectionId.ToString()).SendAsync("MessageReceived", msg[0]);
                            }
                        }
                        return(new OkObjectResult(new { Message = "You Liked", Status = HttpStatusCode.OK }));
                    }
                    else
                    {
                        _bContext.Likes.Attach(lks);
                        _bContext.Likes.Remove(lks);
                        await _bContext.SaveChangesAsync();

                        return(new OkObjectResult(new { Message = "You Remove Liked", Status = HttpStatusCode.OK }));
                    }
                }
                else
                {
                    return(new OkObjectResult(new { Message = "", Status = HttpStatusCode.BadRequest }));
                }
            }
            catch (Exception Ex)
            {
                return(new OkObjectResult(new { Message = "", Error = Ex.Message.ToString(), Status = HttpStatusCode.InternalServerError }));
            }
        }
Exemplo n.º 6
0
 public IActionResult GetPartQstAndAns([FromBody] DParameter Dp)
 {
     try
     {
         _bContext = new BloggingContext();
         var Rslt = GetCrntUserNonayc(HttpContext);
         if (int.TryParse(Dp.Id.ToString(), out int n))
         {
             int Qid = Convert.ToInt32(Dp.Id.ToString());
             var chk = _bContext.Questions.FirstOrDefault(qs => qs.Qid == Qid);
             if (chk != null)
             {
                 var Qstion = (from qst in _bContext.Questions.ToList()
                               join Usr in _bContext.Users.ToList() on qst.AskedBy equals Usr.UsId
                               let Tgs = new SpltTgs {
                     id = qst.Qid, TagsID = qst.Tags.Split(',').ToList()
                 }
                               where qst.Qid == Qid
                               select new
                 {
                     QstId = qst.Qid,
                     Title = qst.Title,
                     Question = qst.Question,
                     Askedby = Rslt != null ? (Rslt.UsId == Usr.UsId ? "You" : Usr.FirstName + " " + Usr.LastName.ToString()) : Usr.FirstName + " " + Usr.LastName.ToString(),
                     AskedOn = qst.AskedOn,
                     Tags = _bContext.Tags.ToList().Where(zx => Tgs.TagsID.Any(x => x.ToString() == zx.TgId.ToString())).Select(x => x.Tagname).ToList(),
                     AUID = qst.AskedBy,
                     Tagsid = qst.Tags
                 }).Take(1).ToList();
                 var Answers = (from Ans in _bContext.Answers.ToList()
                                join Usr in _bContext.Users on Ans.AnswredBy equals Usr.UsId
                                where Ans.Qid == Qid
                                select new
                 {
                     AnsID = Ans.Aid,
                     Answer = Ans.Answer,
                     Answeredby = Rslt != null ? (Rslt.UsId == Usr.UsId ? "You" : Usr.FirstName + " " + Usr.LastName.ToString()) : Usr.FirstName + " " + Usr.LastName.ToString(),
                     Answeredon = Ans.AnswredOn,
                     aid = Ans.AnswredBy,
                     TLikes = _bContext.Likes.Where(x => x.AnsId == Ans.Aid).Count(),
                     ULikes = Rslt != null ? (_bContext.Likes.ToList().Where(x => x.Likeby == Rslt.UsId && x.AnsId == Ans.Aid).ToList().Count()) > 0 : true ? false : false
                 }).OrderBy(x => x.AnsID).ToList();
                 var Contributors = (from usrs in _bContext.Users.ToList()
                                     where Answers.Any(ans => ans.aid == usrs.UsId)
                                     select new
                 {
                     Name = usrs.FirstName + "" + usrs.LastName,
                     pic = Request.Scheme + "://" + Request.Host.Value + (usrs.ImagePath == null ? Url.Content($"/Contents/Images/Defaultprof.png") : Url.Content($"/Contents/Images/{usrs.ImagePath}")),
                 }).ToList();
                 var tags_ = new SpltTgs {
                     id = 0, TagsID = Qstion[0].Tagsid.Split(',').ToList()
                 };
                 var similarqst = (from qst in _bContext.Questions.ToList()
                                   join Usr in _bContext.Users.ToList() on qst.AskedBy equals Usr.UsId
                                   let Tgs = new SpltTgs {
                     id = qst.Qid, TagsID = qst.Tags.Split(',').ToList()
                 }
                                   where Tgs.TagsID.Any(x => tags_.TagsID.Any(y => y.ToString() == x.ToString())) && qst.Qid != Qstion[0].QstId
                                   select new
                 {
                     QstId = qst.Qid,
                     Title = qst.Title,
                     Tags = _bContext.Tags.ToList().Where(zx => Tgs.TagsID.Any(x => x.ToString() == zx.TgId.ToString())).Select(x => x.Tagname).ToList()
                 }).ToList();
                 return(new OkObjectResult(new { Question = Qstion[0], Answers = Answers, Contrib = Contributors, Similar = similarqst, Message = "", Status = HttpStatusCode.OK }));
             }
             else
             {
                 return(new OkObjectResult(new { Message = "Invalid", Status = HttpStatusCode.NotFound }));
             }
         }
         else
         {
             return(new OkObjectResult(new { Message = "Invalid", Status = HttpStatusCode.NotFound }));
         }
     }
     catch (Exception Ex)
     {
         return(new OkObjectResult(new { Message = Ex.Message.ToString(), Status = HttpStatusCode.InternalServerError }));
     }
 }