public async Task <ServiceResponse <GetUserDTO> > AddUser(string userToken, AddUserDTO newUser) { ServiceResponse <GetUserDTO> serviceResponse = new ServiceResponse <GetUserDTO>(); try { var userInformation = await _auth0Service.GetUserInfo(userToken); if (userInformation.ContainsKey("sub") && userInformation.ContainsKey("name") && userInformation.ContainsKey("email")) { var userId = userInformation["sub"].ToString(); if (await _context.Users.FindAsync(userId) == null) { var user = _mapper.Map <User>(newUser); user.Id = userId; user.Name = userInformation["name"].ToString(); user.Mail = userInformation["email"].ToString(); user.CreatedAt = DateTime.Now.ToUniversalTime(); await _context.Users.AddAsync(user); await _context.SaveChangesAsync(); serviceResponse.Data = _mapper.Map <GetUserDTO>(user); } else { serviceResponse.Success = false; serviceResponse.Message = $"User with id '{userId}' already exists."; } } else { serviceResponse.Success = false; serviceResponse.Message = $"Could not get all needed user information from auth0."; } } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Exception = ex.Message; } return(serviceResponse); }
public async Task <ServiceResponse <GetReservationDTO> > AddReservation(string userToken, AddReservationDTO newReservation) { ServiceResponse <GetReservationDTO> serviceResponse = new ServiceResponse <GetReservationDTO>(); try { var user = await _context.Users.FindAsync(newReservation.UserId); if (user == null) { var userInformation = await _auth0Service.GetUserInfo(userToken); // check if user is verified if (userInformation.ContainsKey("email_verified") && !Convert.ToBoolean(userInformation["email_verified"].ToString())) { serviceResponse.Success = false; serviceResponse.Message = $"User email is not verified."; return(serviceResponse); } // check if data from auth0 are valid if (!userInformation.ContainsKey("sub") || !userInformation.ContainsKey("name") || !userInformation.ContainsKey("email")) { serviceResponse.Success = false; serviceResponse.Message = $"Could not get all needed user information from auth0."; return(serviceResponse); } user = new User(); user.Id = userInformation["sub"].ToString(); user.Name = userInformation["name"].ToString(); user.Mail = userInformation["email"].ToString(); user.CreatedAt = DateTime.Now.ToUniversalTime(); // user is added to context, but NOT saved to db at this point await _context.Users.AddAsync(user); } var marketSeller = await _context.MarketSellers.Include(x => x.Market).Include(x => x.Seller).FirstOrDefaultAsync(x => x.SellerId == newReservation.SellerId && x.MarketId == newReservation.MarketId); if (marketSeller != null) { // get next market day var next = DateTime.Now.GetNextDate((DayOfWeek)marketSeller.Market.DayOfWeek); // get start and end time on the next market day var nextStart = next.SetTime(marketSeller.Market.StartTime, DateTimeKind.Local); var nextEnd = next.SetTime(marketSeller.Market.EndTime, DateTimeKind.Local); // get the time until the seller will accepted reservations var lastReservation = nextStart.AddHours(marketSeller.LastReservationOffset ?? 0); // check if seller is still accepting reservations if (DateTime.Now <= lastReservation) { if (newReservation.Pickup >= nextStart.ToUniversalTime() && newReservation.Pickup <= nextEnd.ToUniversalTime()) { Reservation reservation = _mapper.Map <Reservation>(newReservation); reservation.MarketSellerId = marketSeller.Id; reservation.CreatedAt = DateTime.Now.ToUniversalTime(); // add information to user obj if user would be rembered if (newReservation.RememberMe) { user.Firstname = newReservation.Firstname; user.Lastname = newReservation.Lastname; user.Phone = newReservation.Phone; } // add and save reservation (this also saves the user created above) await _context.Reservations.AddAsync(reservation); await _context.SaveChangesAsync(); // load references to return a complete reservation object _context.Entry(reservation).Reference(x => x.MarketSeller).Load(); _context.Entry(reservation.MarketSeller).Reference(x => x.Market).Load(); _context.Entry(reservation.MarketSeller).Reference(x => x.Seller).Load(); // send user mail await _mailService.SendReservationConfirmation(user.Mail, CreatePlaceholders(reservation, marketSeller.Market, marketSeller.Seller)); // send seller mail // TODO: add seller mail serviceResponse.Data = _mapper.Map <GetReservationDTO>(reservation); } else { serviceResponse.Success = false; serviceResponse.Message = $"Pickup invalid! It is only possible to create reservations for the next market day (Start: {nextStart.ToString()}, End: {nextEnd.ToString()})."; } } else { serviceResponse.Success = false; serviceResponse.Message = $"Reservation time limit exceeded for seller (id: '{newReservation.SellerId}') at market (id: '{newReservation.MarketId}'. The seller allows reservations only until {marketSeller.LastReservationOffset} minutes before market starts."; } } else { serviceResponse.Success = false; serviceResponse.Message = $"Could not found combination of marketId '{newReservation.MarketId}' and sellerId '{newReservation.SellerId}'"; } } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Exception = ex.Message; } return(serviceResponse); }