Beispiel #1
0
        public async Task <IActionResult> GetUser(long id)
        {
            ActionResult <User> action = await userDA.GetUser(id);

            if (action == null || action.Value == null)
            {
                return(new NotFoundResult());
            }
            User    user    = action.Value;
            UserDTO userDTO = ItemToDTO(user);

            return(new OkObjectResult(userDTO));
        }
Beispiel #2
0
        /*public async Task<IActionResult> GetTicketUser(long id)
         * {
         *  ActionResult<TicketUser> action = await ticketUserDA.GetTicketUser(id);
         *
         *  if (action == null || action.Value == null)
         *  {
         *      return new NotFoundResult();
         *  }
         *
         *  TicketUser ticketUser = action.Value;
         *
         *  // Get total punches
         *  ActionResult<TicketStore> ticketStoreAction = await ticketStoreDA.GetTicketStore(ticketUser.TicketStoreId);
         *  if (ticketStoreAction == null || ticketStoreAction.Value == null)
         *  {
         *      return new NotFoundResult();
         *  }
         *  TicketStore ticketStore = ticketStoreAction.Value;
         *
         *  // Get store name
         *  ActionResult<Store> storeAction = await storeDA.GetStore(ticketStore.StoreId);
         *  if (storeAction == null || storeAction.Value == null)
         *  {
         *      return new NotFoundResult();
         *  }
         *  Store store = storeAction.Value;
         *
         *  // Get ticket type
         *  ActionResult<TicketType> ticketTypeAction = await ticketTypeDA.GetTicketType(ticketStore.TicketTypeId);
         *  if (ticketTypeAction == null || ticketTypeAction.Value == null)
         *  {
         *      return new NotFoundResult();
         *  }
         *  TicketType ticketType = ticketTypeAction.Value;
         *
         *  TicketUserDTO ticketUserDTO = ItemToDTO(ticketUser, ticketStore.TotalPunches, store.Name, ticketType.Id);
         *
         *  return new OkObjectResult(ticketUserDTO);
         * }*/
        public async Task <ActionResult <TicketUserDTO> > CreateTicketUser(IIdentity userIdentity, TicketUser ticketUser,
                                                                           int userTempCode, string userEmail, IHubContext <ChatHub> hub)
        {
            string userIdStr = null;
            long   userId    = 0;
            User   user      = null;

            // Check if Cash ticket
            ActionResult <TicketStore> action = await ticketStoreDA.GetTicketStore(ticketUser.TicketStoreId);

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

            // Cash ticket
            if (ticketStore.TicketTypeId == CASH_TICKET)
            {
                if (string.IsNullOrEmpty(userEmail))
                {
                    return(new ConflictObjectResult("Please pass email"));
                }

                ActionResult <User> actionUser = await userDA.GetUser(userEmail);

                if (actionUser == null || actionUser.Value == null)
                {
                    //return new NotFoundResult();
                    return(new NotFoundObjectResult("user id : " + userId + " not found"));
                }
                user = actionUser.Value;

                userId = user.Id;
                //ticketUser.UserId = userId;
            }
            // Regular ticket
            else
            {
                userIdStr = Identity.GetValueFromClaim(userIdentity, "Id");
                userId    = Convert.ToInt64(userIdStr);
                //ticketUser.UserId = userId;
            }

            ticketUser.UserId = userId;

            #region// Get ticket user
            //TicketUser existTicketUser = null;
            ActionResult <TicketUser> actionTicketUser = await ticketUserDA.GetTicketUser(userId,
                                                                                          ticketUser.TicketStoreId, TICKET_ACTIVE);

            if (actionTicketUser != null && actionTicketUser.Value != null)
            {
                return(new ConflictObjectResult("The ticket already exists and active"));
            }
            #endregion

            if (ticketStore.TicketTypeId == CASH_TICKET)
            {
                if (userTempCode == EMPTY_CODE)
                {
                    return(new ConflictObjectResult("Please generate code and pass him"));
                }

                /*if (string.IsNullOrEmpty(userEmail))
                 * {
                 *  return new ConflictObjectResult("Please pass email");
                 * }*/
                /*ActionResult<User> actionUser = await userDA.GetUser(userId);
                 * if (actionUser == null || actionUser.Value == null)
                 * {
                 *  //return new NotFoundResult();
                 *  return new NotFoundObjectResult("user id : " + userId + " not found");
                 * }*/
                /*ActionResult<User> actionUser = await userDA.GetUser(userEmail);
                 * if (actionUser == null || actionUser.Value == null)
                 * {
                 *  //return new NotFoundResult();
                 *  return new NotFoundObjectResult("user id : " + userId + " not found");
                 * }
                 * User user = actionUser.Value;*/

                if (user.TempCode != userTempCode)
                {
                    return(new ConflictObjectResult("Wrong temp code"));
                }
                if (user.CreatedTempCode.Value.AddMinutes(5) < DateTime.Now)
                {
                    return(new NotFoundObjectResult("More than five minutes have passed, please try again"));
                }

                // Remove tempCode and createdTempCode from database
                user.TempCode        = null;
                user.CreatedTempCode = null;

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

            await ticketUserDA.CreateTicketUser(ticketUser);

            // Caller chat notification
            await hub.Clients.All.SendAsync(ticketUser.UserId.ToString(), "Ticket user created successfully", ticketUser);

            ActionResult <List <TicketUserJoinTicketStoreJoinStore> > actionJoin =
                await ticketUserDA.GetTicketUserWithJoin(userId, ticketUser.TicketStoreId);

            if (actionJoin == null || actionJoin.Value == null)
            {
                return(new NotFoundResult());
            }
            List <TicketUserJoinTicketStoreJoinStore> ticketJoin = actionJoin.Value;

            return(new CreatedAtRouteResult(new { Id = ticketUser.Id }, ticketJoin[0]));
        }