Ejemplo n.º 1
0
        public async Task<IActionResult> Comment(string id)
        {
            var user = await Service.GetCurrentUserAsync();
            if (user.UserName == id)
            {
                return RedirectToAction(nameof(AccountController.Index), "Account");
            }

            RatingRecord model = null;
            var supUser = await DbContext.LoginInfos.Include(l => l.UserInfo).FirstOrDefaultAsync(l => l.UserName == id);
            if (supUser != null)
            {
                model = await DbContext.RatingRecords.FirstOrDefaultAsync(r =>
                    r.UserName == user.UserName && r.SupporterName == supUser.UserName);
                if (model == null)
                {
                    model = new RatingRecord
                    {
                        UserName = user.UserName,
                        SupporterName = supUser.UserName,
                        Rating = 0,
                        RatingMsg = "",
                        Time = System.DateTime.Now,
                        SupporterInfo = supUser
                    };
                }
                else
                {
                    model.SupporterInfo = supUser;
                }
            }
            ViewData["RatingValid"] = "";
            return View(model);
        }
Ejemplo n.º 2
0
        public async Task<IActionResult> Comment(string userName, int rating, string comment)
        {
            var user = await Service.GetCurrentUserAsync();
            if (user.UserName == userName)
            {
                return new BadRequestResult();
            }

            RatingRecord model = null;
            var supUser = await DbContext.LoginInfos.Include(l => l.UserInfo)
                .FirstOrDefaultAsync(l => l.UserName == userName);
            if (supUser != null)
            {
                if (rating > 5 || rating < 1)
                {
                    ViewData["RatingValid"] = "评分不可为空!";
                    return View(new RatingRecord
                    {
                        UserName = user.UserName,
                        SupporterName = supUser.UserName,
                        Rating = 0,
                        RatingMsg = comment,
                        Time = System.DateTime.Now,
                        SupporterInfo = supUser
                    });
                }

                ViewData["RatingValid"] = "";
                model = await DbContext.RatingRecords.FirstOrDefaultAsync(r =>
                    r.UserName == user.UserName && r.SupporterName == supUser.UserName);
                if (model == null)
                {
                    model = new RatingRecord
                    {
                        UserName = user.UserName,
                        SupporterName = supUser.UserName,
                        Rating = rating,
                        RatingMsg = comment,
                        Time = System.DateTime.Now
                    };
                    DbContext.RatingRecords.Add(model);
                }
                else
                {
                    model.Rating = rating;
                    model.RatingMsg = comment;
                    model.Time = System.DateTime.Now;
                    DbContext.RatingRecords.Update(model);
                }
                model.SupporterInfo = supUser;
                try
                {
                    await DbContext.SaveChangesAsync();
                    ViewData["IsSucceed"] = true;
                    ViewData["Msg"] = "提示:您的评论已成功提交~";
                }
                catch
                {
                    ViewData["IsSucceed"] = false;
                    ViewData["Msg"] = "在提交您的评论时出现了错误!";
                }
            }
            return View(model);
        }