public async Task <ActionResult <TicketUserDTO> > GenerateTempCode(IIdentity userIdentity) { int tempCode = 0; #region// Get user id string userIdStr = Identity.GetValueFromClaim(userIdentity, "Id"); long userId = Convert.ToInt64(userIdStr); #endregion ActionResult <User> action = await userDA.GetUser(userId); if (action == null || action.Value == null) { //return new NotFoundResult(); return(new NotFoundObjectResult("user id : " + userId + " not found")); } User user = action.Value; /*if ( (user.CreatedTempCode != null) && (user.CreatedTempCode.Value.AddMinutes(1) >= DateTime.Now) ) * { * return new NotFoundObjectResult("please wait one minute and try again"); * }*/ // Generate temp code(1 - 99999) and insert to ticket user(database) tempCode = new Random().Next(10001, 100000); // Insert temp code to user user.TempCode = tempCode; user.CreatedTempCode = DateTime.Now; #region// Insert to database try { await userDA.PutUser(user); } catch (DbUpdateConcurrencyException) when(!userDA.Exists(user.Id)) { return(new NotFoundResult()); } #endregion return(new OkObjectResult( ItemToDTO(user))); }
/*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])); }