Example #1
0
        public async Task SaveTicket(LotteryTicketDto ticketDto, string userName)
        {
            var user = await _userMngr.FindByNameAsync(userName);

            if (user != null)
            {
                var ticket = await _lotteryTicketRepository.Get(ticketDto.TicketNumber, ticketDto.Week, user.Id);

                if (ticket == null)
                {
                    var ticketModel = new LotteryTicket
                    {
                        IsPaid         = false,
                        IsWinnerTicket = false,
                        Purchased      = DateTime.UtcNow,
                        Week           = ticketDto.Week,
                        TicketNumber   = ticketDto.TicketNumber,
                        RowUpdated     = DateTime.UtcNow,
                        User           = user
                    };

                    await _lotteryTicketRepository.Insert(ticketModel);
                }
                else
                {
                    ticket.IsPaid         = ticket.IsPaid;
                    ticket.IsWinnerTicket = ticket.IsWinnerTicket;
                    ticket.RowUpdated     = DateTime.UtcNow;

                    await _lotteryTicketRepository.Update(ticket);
                }
            }
        }
Example #2
0
        public async Task <IActionResult> PurchaseTicket([FromBody] LotteryTicketDto ticketDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Please make sure all fields are properly supplied"));
            }

            var userNameType = User.Claims.FirstOrDefault(x => x.Type == "Username");

            if (userNameType != null)
            {
                await _lotteryTicketService.SaveTicket(ticketDto, userNameType.Value);

                return(NoContent());
            }
            return(BadRequest("Username does not exist"));
        }