public Task <ActionResult <VoteView> > AddVote([FromRoute] long contentId, [FromBody] VoteType vote)
    {
        //A nice shortcut in case users do it this way
        if (vote == VoteType.none)
        {
            return(DeleteVote(contentId));
        }

        return(MatchExceptions(async() =>
        {
            RateLimit(RateInteract);
            var uid = GetUserIdStrict();

            VoteView writeVote;

            //Try to lookup the existing vote to update it, otherwise create a new one
            try
            {
                writeVote = await shortcuts.LookupVoteByContentIdAsync(uid, contentId);
            }
            catch (NotFoundException)
            {
                writeVote = new VoteView()
                {
                    contentId = contentId
                };
            }

            //Actually set the vote to what they wanted
            writeVote.vote = vote;

            return await CachedWriter.WriteAsync(writeVote, uid); //message used for activity and such
        }));
    }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取打分表信息,绑定到表格
        /// </summary>
        public void BindGrid()
        {
            Maticsoft.BLL.dafenbiao dfb = new BLL.dafenbiao();
            DataSet ds = dfb.GetList(10, "", "id");

            VoteView.DataSource = ds;
            VoteView.DataBind();
        }
        /// <summary>
        /// 获取打分表信息,绑定到表格
        /// </summary>
        public void BindGrid()
        {
            Maticsoft.BLL.dafenbiao dfb = new BLL.dafenbiao();
            DataSet ds = dfb.GetList("id>=13 and id <= 20 order by id");

            VoteView.DataSource = ds;
            VoteView.DataBind();
            DataSet ds2 = dfb.GetList("id=21");

            View_yiyuan.DataSource = ds2;
            View_yiyuan.DataBind();
        }
Ejemplo n.º 4
0
        public async Task <IHttpActionResult> PostVote(int id, VoteView voteView)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var entry = _db.Entries.Include(x => x.Votes).Include(x => x.User).SingleOrDefault(x => x.Id == id);

            if (entry == null)
            {
                return(NotFound());
            }

            var userVoteResult = EntryHelper.UserVoteResult(User.Identity, entry.Votes);

            if (userVoteResult != null)
            {
                return(BadRequest());
            }

            var userName = UserHelper.GetUserNameFromIdentity(User.Identity);
            var user     = _userContext.GetUser(userName);

            var vote = new Vote
            {
                Author     = UserHelper.GetUserNameFromIdentity(User.Identity),
                CreateDate = DateTime.Now,
                Entry      = entry,
                IsPositive = voteView.IsPositive,
                UserId     = user.Id
            };

            _db.Votes.Add(vote);
            await _db.SaveChangesAsync();

            _mailer.InformAboutVote(entry);

            var location = $"api/entry/{vote.Entry.Id}";

            return(Created(location, new VoteView
            {
                Id = vote.Id,
                Author = _userContext.GetUser(UserHelper.GetUserNameFromComplexUsername(vote.Author)).Name,
                CreateDate = vote.CreateDate,
                IsPositive = voteView.IsPositive
            }));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 获取打分表信息,绑定到表格
        /// </summary>
        public void BindGrid()
        {
            Maticsoft.BLL.dafenbiao dfb = new BLL.dafenbiao();
            int     style = Convert.ToInt16(Request.QueryString["style"]);
            DataSet ds    = new DataSet();

            if (style == 1)
            {
                ds = dfb.GetList("id>=11 and id<=12 order by id");
            }
            else
            {
                ds = dfb.GetList("id>=22 order by id");
            }
            VoteView.DataSource = ds;
            VoteView.DataBind();
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> CreateVote([FromBody] VoteView voteView)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var vote     = mapper.Map <VoteView, Vote>(voteView);
            var clientId = HttpContext.User.Claims.First().Value;

            vote.ClientId = new Guid(clientId);
            await context.Votes.AddAsync(vote);

            await context.SaveChangesAsync();

            var result = mapper.Map <Vote, VoteView>(vote);

            return(Ok(result));
        }
Ejemplo n.º 7
0
        public async Task <VoteView> Get(int productId, string UserId)
        {
            try
            {
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@ProductId", productId);
                parameters.Add("@UserId", UserId);
                var result = await SqlMapper.QueryFirstAsync <VoteView>(cnn : connection, sql : "sp_GetVote",
                                                                        param : parameters, commandType : CommandType.StoredProcedure);

                return(result);
            }
            catch (Exception ex)
            {
                var vote = new VoteView();
                vote.ProductId = 0;
                return(vote);
            }
        }
    public async Task LookupVoteByContentId_Simple(long uid, long cid)
    {
        //Shouldn't exist at first... we hope?
        await Assert.ThrowsAnyAsync <NotFoundException>(() => service.LookupVoteByContentIdAsync(uid, cid));

        var vote = new VoteView()
        {
            contentId = cid, vote = VoteType.ok
        };
        var writtenVote = await writer.WriteAsync(vote, uid);

        Assert.Equal(vote.vote, writtenVote.vote);
        Assert.Equal(vote.contentId, writtenVote.contentId);

        //now go look it up
        var lookupVote = await service.LookupVoteByContentIdAsync(uid, cid);

        Assert.Equal(uid, lookupVote.userId);
        Assert.Equal(cid, lookupVote.contentId);
        Assert.Equal(vote.vote, lookupVote.vote);
    }
Ejemplo n.º 9
0
        public JsonResult SaveVote(VoteView request)
        {
            var result = ApiHelper <Result> .HttpPostAsync($"Vote/Save", "POST", request);

            return(Json(new { data = result }));
        }
Ejemplo n.º 10
0
 public Task <ActionResult <VoteView> > WriteVoteAsync([FromBody] VoteView vote) =>
 MatchExceptions(() => WriteAsync(vote));
Ejemplo n.º 11
0
        public async Task <HttpResponseMessage> VoteDish([FromBody] VoteView model)
        {
            if (model == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No Data"));
            }
            ApplicationUser user = await users.FindByNameAsync(User.Identity.Name);

            if (user == null)
            {
                throw new HttpResponseException(HttpStatusCode.MethodNotAllowed);
            }
            if (ModelState.IsValid)
            {
                Vote item = new Vote
                {
                    DishId  = model.DishId,
                    UserId  = user.Id,
                    Like    = model.Like,
                    Created = DateTime.Now
                };
                db.Votes.AddOrUpdate(item);
                await db.SaveChangesAsync();

                Dish dish = await db.Dishes.Include(x => x.Categories).Include(x => x.Pictures).SingleOrDefaultAsync(i => i.Id == model.DishId);

                dish.Yes = db.Votes.Where(x => x.DishId == dish.Id && x.Like == true).Count();
                dish.No  = db.Votes.Where(x => x.DishId == dish.Id && x.Like == false).Count();
                await db.SaveChangesAsync();

                if (dish == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                return(Request.CreateResponse(HttpStatusCode.OK, new DishView
                {
                    Id = dish.Id,
                    Name = dish.Name,
                    Price = dish.Price,
                    isVeganFriendly = dish.isVeganFriendly,
                    Yes = dish.Yes,
                    No = dish.No,
                    Created = dish.Created,
                    Categories = dish.Categories.Select(c => new CategoryView
                    {
                        Id = c.Id,
                        Name = c.Name,
                    }).ToList(),
                    Pictures = dish.Pictures.Select(c => new PictureView
                    {
                        Id = c.Id,
                        Name = c.Name,
                        Route = c.Route,
                        Created = c.Created,
                    }).ToList(),
                    Vote = new VoteView
                    {
                        DishId = item.DishId,
                        Like = item.Like,
                        Created = item.Created
                    }
                }));
            }
            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
        }