コード例 #1
0
        public async Task<IHttpActionResult> Get(string accountPhone)
        {
            if (!accountPhoneRegex.IsMatch(accountPhone))
                return BadRequest("The \"accountPhone\" must be a 10-digit phone number.");

            var today = DateTime.Today;

            using (var db = new Entities())
            {
                var account = await db.Accounts.FirstOrDefaultAsync(a => a.Phone == accountPhone);

                if (account == null)
                {
                    return BadRequest(string.Format(
                        "The \"{0}\" accountPhone is invalid!", accountPhone));
                }

                var drawing = await db.Drawings.FirstOrDefaultAsync(d =>
                    d.Account.Phone == accountPhone && d.Date == today);

                if (drawing == null)
                {
                    return BadRequest(string.Format(
                        "There is no {0} {1:MM/dd/yyyy} drawing.", account.Name, today));
                }

                var prizes = await (from p in db.Prizes
                                    where p.DrawingId == drawing.Id
                                    select new
                                    {
                                        PrizeId = p.Id,
                                        PrizeName = p.Name,
                                        Provider = p.Provider,
                                        Quantity = p.Quantity - p.Winners.Count()
                                    }).ToListAsync();

                var prizeInfos = new List<PrizeInfo>();

                foreach (var prize in prizes)
                {
                    for (int i = 0; i < prize.Quantity; i++)
                    {
                        prizeInfos.Add(new PrizeInfo()
                        {
                            Guid = Guid.NewGuid(),
                            Name = prize.PrizeName,
                            PrizeId = prize.PrizeId,
                            Provider = prize.Provider
                        });
                    }
                }

                if (prizeInfos.Count == 0)
                    return Ok<WinnerInfo>(null);

                var random = new Random();

                var tickets = await (from t in db.Tickets
                                     where t.DrawingId == drawing.Id && t.Winners.Count == 0
                                     select t).ToListAsync();

                if (tickets.Count == 0)
                    return Ok<WinnerInfo>(null);

                var ticket = tickets[random.Next(tickets.Count)];

                var prizeInfo = prizeInfos.
                    OrderBy(i => i.Guid).ToList()[random.Next(prizeInfos.Count)];

                var winner = new Winner()
                {
                    PrizeId = prizeInfo.PrizeId,
                    TicketId = ticket.Id,
                    WonOn = DateTime.UtcNow
                };

                db.Winners.Add(winner);

                await db.SaveChangesAsync();

                var winnerInfo = new WinnerInfo()
                {
                    PrizeName = prizeInfo.Name,
                    PrizeProvider = prizeInfo.Provider,
                    TicketName = ticket.Name,
                    TicketEmail = ticket.Email,
                    TicketPhone = ticket.Phone,
                    AccountPhone = account.Phone,
                    AccountName = account.Name,
                    DrawingDate = drawing.Date,
                    PrizesLeft = prizeInfos.Count - 1,
                    TicketsLeft = tickets.Count - 1
                };

                return Ok(winnerInfo);
            }
        }
コード例 #2
0
        public async Task<HttpResponseMessage> Post([FromBody]FormDataCollection formData)
        {
            const string BADPHONE = "An SMS message with an invalid \"{0}\" number was received!";

            const string REGISTERED =
                "You're registered to win a door prize in {0}'s {1:MM/dd/yyyy} drawing.  Good luck!!";

            const string DRAWINGCLOSED =
                "{0}'s {1:MM/dd/yyyy} door prize drawing is closed and can accept no more entries!";

            const string BADBODY =
                "To register to win a door prize, please send an SMS in the format EMAIL,NAME (i.e. [email protected],Some Dude) to {0}.";

            var to = formData["To"];

            if (!phoneRegex.IsMatch(to))
                return GetResponse(BADPHONE, "To");

            var from = formData["From"];

            if (!phoneRegex.IsMatch(from))
                return GetResponse(BADPHONE, "From");

            var body = formData["Body"];

            if (string.IsNullOrWhiteSpace(body))
                return GetResponse(BADBODY, from);

            var parts = body.Split(',', ';', '|');

            if (parts.Length != 2)
                return GetResponse(BADBODY, from);

            var email = parts[0].Trim().ToLower();
            var name = parts[1].Trim().ToUpper();

            if (!emailRegex.IsMatch(email))
                return GetResponse(BADBODY, from);

            if (string.IsNullOrWhiteSpace(name))
                return GetResponse(BADBODY, from);

            to = to.Substring(2);
            from = from.Substring(2);

            using (var db = new Entities())
            {
                var account = await db.Accounts.FirstOrDefaultAsync(a => a.Phone == to);

                if (account == null)
                    return GetResponse(BADPHONE, "To");

                var drawing = await db.Drawings.FirstOrDefaultAsync(
                    c => c.AccountId == account.Id && c.Date == DateTime.Today);

                if (drawing == null)
                {
                    drawing = new Drawing()
                    {
                        AccountId = account.Id,
                        Date = DateTime.Today
                    };

                    db.Drawings.Add(drawing);

                    await db.SaveChangesAsync();
                }

                if (await db.Winners.AnyAsync(w => w.Prize.DrawingId == drawing.Id))
                    return GetResponse(DRAWINGCLOSED, account.Name, drawing.Date);

                var ticket = await db.Tickets.FirstOrDefaultAsync(t =>
                    t.DrawingId == drawing.Id && t.Phone == from);

                if (ticket == null)
                {
                    ticket = new Ticket()
                    {
                        DrawingId = drawing.Id,
                        Email = email,
                        Name = name,
                        Phone = from
                    };

                    db.Tickets.Add(ticket);
                }
                else
                {
                    ticket.Name = name;
                    ticket.Email = email;
                }

                await db.SaveChangesAsync();

                int prizesLeft = 0;
                int ticketsLeft = 0;

                var prizes = await (from p in db.Prizes
                                    where p.DrawingId == drawing.Id
                                    select new
                                    {
                                        Quantity = p.Quantity - p.Winners.Count()
                                    }).ToListAsync();

                foreach (var prize in prizes)
                {
                    for (int i = 0; i < prize.Quantity; i++)
                        prizesLeft++;
                }

                ticketsLeft = await (from t in db.Tickets
                                     where t.DrawingId == drawing.Id && t.Winners.Count == 0
                                     select t).CountAsync();

#pragma warning disable CS4014
                Task.Run(() => PublishDrawingInfo(drawing, prizesLeft, ticketsLeft));
#pragma warning restore CS414

                return GetResponse(REGISTERED, account.Name, drawing.Date);
            }
        }