Пример #1
0
        async public Task <JsonResult> SaveDraw(string setCode, string data)
        {
            // See if the user already has a userDrawId. If not, create one for them.
            // This (will be used) to tie a user's draws together so they can see a list of them.
            var userDrawId = HttpContext.Request.Cookies["userDrawId"];

            if (string.IsNullOrWhiteSpace(userDrawId))
            {
                // A GUID to hold the cartId.
                userDrawId = Guid.NewGuid().ToString();

                // Send cart Id as a cookie to the client.
                HttpContext.Response.Cookies.Append("userDrawId", userDrawId);
            }

            var drawEntity = new DrawEntity()
            {
                SetCode    = setCode,
                Results    = data,
                UserDrawId = userDrawId
            };
            var uniqueId = await _storageContext.SaveDraw(drawEntity);

            // This will return something like: http://localhost:1491/ogw?draw=TvizXlV
            var returnJson = $"{{ \"drawId\": \"{uniqueId}\", \"url\": \"/{setCode}?draw={uniqueId}\" }}";

            return(Json(returnJson));
        }
Пример #2
0
        public DrawResult DrawWinner()
        {
            var generator = new Random();

            var latestLottery = _ctx.LotteryEntity.OrderByDescending(l => l.Id)
                                .Include(l => l.Tickets)
                                .ThenInclude(t => t.User)
                                .Include(l => l.LoteryCharity)
                                .ThenInclude(c => c.Charity)
                                .First();

            var pool          = latestLottery.Tickets.OrderBy(t => t.Id).ToList();
            var winningNumber = generator.Next(pool.Count);
            var winingTicket  = pool[winningNumber];
            var winningUser   = winingTicket.User;
            var voteResult    = pool.GroupBy(p => p.CharityId)
                                .Select(g => new
            {
                CharityId = g.Key,
                Votes     = g.Count()
            }).OrderByDescending(c => c.Votes)
                                .First();

            var winningCharity = this._ctx.CharityEntity.First(c => c.Id == voteResult.CharityId);

            var result = new DrawResult
            {
                TotalParticipants = pool.Count,
                Ticket            = new DrawTicket()
                {
                    Id   = winingTicket.Id,
                    Name = winingTicket.Name
                },
                User = new DrawnUser()
                {
                    Name = winningUser.Name,
                    Id   = winningUser.Id
                },
                Charity = new DrawCharity
                {
                    Id   = winningCharity.Id,
                    Name = winningCharity.Name
                }
            };

            var drawEntity = new DrawEntity()
            {
                TicketId = winingTicket.Id
            };

            _ctx.Draws.Add(drawEntity);
            _ctx.SaveChanges();
            return(result);
        }