Exemplo n.º 1
0
        public async Task <int> CreatePunchHistory(PunchHistory punchHistory)
        {
            using var context = new DevTicketDatabaseContext(DevTicketDatabaseContext.ops.dbOptions);

            context.PunchingHistory.Add(punchHistory);
            return(await context.SaveChangesAsync());
        }
Exemplo n.º 2
0
        public async Task <ActionResult <TicketUserDTO> > CreatePunch(long ticketStoreId, int tempCode,
                                                                      IHubContext <ChatHub> hub)
        {
            #region// Get ticket user with temp code
            ActionResult <TicketUser> actionTicketUser = await ticketUserDA.GetTicketUser(tempCode, ticketStoreId);

            if (actionTicketUser == null || actionTicketUser.Value == null)
            {
                return(new NotFoundObjectResult("Wrong temp code "));
            }
            TicketUser ticketUser = actionTicketUser.Value;

            if (ticketUser.CreatedTempCode.Value.AddMinutes(5) < DateTime.Now)
            {
                return(new NotFoundObjectResult("More than five minutes have passed, please try again"));
            }
            #endregion

            #region// Get ticket store
            ActionResult <TicketStore> actionStore = await ticketStoreDA.GetTicketStore(ticketStoreId);

            if (actionStore == null || actionStore.Value == null)
            {
                return(new NotFoundResult());
            }
            TicketStore ticketStore = actionStore.Value;
            #endregion

            if (ticketUser.Punch >= ticketStore.TotalPunches || ticketUser.Status == TICKET_UNACTIVE)
            {
                return(new BadRequestObjectResult("Ticket id '" + ticketUser.Id + "' is finish"));
            }

            // Make punch in the ticket
            ticketUser.Punch++;
            ticketUser.LastPunching = DateTime.Now;
            // Remove tempCode and createdTempCode from database
            ticketUser.TempCode        = null;
            ticketUser.CreatedTempCode = null;

            if (ticketUser.Punch >= ticketStore.TotalPunches)
            {
                ticketUser.Status = TICKET_UNACTIVE;
            }

            #region// Insert to database
            try
            {
                await ticketUserDA.PutTicketUser(ticketUser);
            }
            catch (DbUpdateConcurrencyException) when(!ticketUserDA.Exists(ticketUser.Id))
            {
                return(new NotFoundResult());
            }
            #endregion

            // Get store name
            ActionResult <Store> storeAction = await storeDA.GetStore(ticketStore.StoreId);

            if (storeAction == null || storeAction.Value == null)
            {
                return(new NotFoundResult());
            }
            Store store = storeAction.Value;

            // Caller chat notification
            await hub.Clients.All.SendAsync(ticketUser.UserId.ToString(), "success", ticketUser);

            // Create punch history
            PunchHistory punchHistory = new PunchHistory
            {
                Id            = Guid.NewGuid().ToString(),
                UserId        = ticketUser.UserId,
                TicketStoreId = ticketUser.TicketStoreId,
                Punch         = ticketUser.Punch,
                CreatedDate   = (DateTime)ticketUser.LastPunching
            };
            await punchHistoryDA.CreatePunchHistory(punchHistory);

            return(new OkObjectResult(
                       ItemToDTO(ticketUser, ticketStore.TotalPunches, store.Name, ticketStore.TicketTypeId,
                                 ticketStore.PunchValue, ticketStore.GiftDescription)));
        }