public static string ImportTrains(StationsDbContext context, string jsonString) { var trainsFromJson = JsonConvert.DeserializeObject <TrainDto[]>(jsonString); var result = new StringBuilder(); var validTrains = new List <Train>(); foreach (var train in trainsFromJson) { string trainNumber = train.TrainNumber; var type = Enum.Parse <TrainType>(train.Type ?? "HighSpeed"); var seats = train.Seats; var isTrainNumberValid = !String.IsNullOrWhiteSpace(trainNumber) && trainNumber?.Length <= 10 && !validTrains.Any(t => t.TrainNumber == trainNumber); var areSeatsValid = seats .All(s => context.SeatingClasses .Any(sc => sc.Name == s.Name && sc.Abbreviation == s.Abbreviation) && s.Quantity != null && s.Quantity >= 0); if (!isTrainNumberValid || !areSeatsValid) { result.AppendLine(FailureMessage); continue; } var currentTrain = new Train() { TrainNumber = trainNumber, Type = type, TrainSeats = new List <TrainSeat>() }; foreach (var seat in train.Seats) { var currentSeat = new TrainSeat() { Train = currentTrain, SeatingClass = context.SeatingClasses.SingleOrDefault(sc => sc.Name == seat.Name && sc.Abbreviation == seat.Abbreviation), Quantity = seat.Quantity.Value }; currentTrain.TrainSeats.Add(currentSeat); } validTrains.Add(currentTrain); result.AppendLine(string.Format(SuccessMessage, train.TrainNumber)); } context.AddRange(validTrains); context.SaveChanges(); return(result.ToString().TrimEnd()); }
public JsonResult BookTicket(string TrainId, string TrainSeatType, string DateOfJourney, string NameOfPassenger, string PanNumber, bool IsPaymentSuccessful) { var trainSeat = db.TrainSeats.FirstOrDefault(ts => ts.TrainId.Equals(TrainId) && ts.SeatType.Equals(TrainSeatType) && ts.IsReserved.Equals(false)); if (trainSeat != null) { var passengerInfo = new PassengerInformation { SeatNumber = trainSeat.SeatNumber, PanNumber = PanNumber, NameOfPassenger = NameOfPassenger, IsPaymentSuccessful = IsPaymentSuccessful, IsPaymentReturned = false, IsTicketCanceled = false }; db.PassengerInfo.Add(passengerInfo); db.SaveChanges(); var trainSeatReserve = new TrainSeat { Id = trainSeat.Id, IsReserved = true, SeatNumber = trainSeat.SeatNumber, SeatType = trainSeat.SeatType, TrainId = trainSeat.TrainId, TrainScheduledDateTime = trainSeat.TrainScheduledDateTime }; //db.Entry(trainSeatReserve).State = EntityState.Modified; db.Entry(trainSeat).CurrentValues.SetValues(trainSeatReserve); db.SaveChanges(); } return(Json(new { SeatNumber = trainSeat != null ? trainSeat.SeatNumber : "No seats available", PanNumber = PanNumber, NameOfPassenger = NameOfPassenger, IsPaymentSuccessful = IsPaymentSuccessful, IsPaymentReturned = false, IsTicketCanceled = false, Confirmed = trainSeat != null ? "Yes" : "No" }, JsonRequestBehavior.AllowGet)); }
private void add_Click(object sender, EventArgs e) { Carriage carriage = new Carriage(); carriage.trainType = this.trainType.Text.ToCharArray()[0]; carriage.trainCode = this.trainCode.Text; carriage.carriageNum = int.Parse(this.carriageNum.Text); carriage.carriageType = this.carriageType.Text; carriage.seatCount = int.Parse(this.seatNum.Text); carriage.startDate = DateTime.Parse(this.dateTimePicker1.Text); TrainNum trainNum = getTrainNum(this.dateTimePicker1.Text, this.trainCode.Text); TrainSeat seat = new TrainSeat(); seat.seatType = this.seatType.Text; seat.trainCode = this.trainCode.Text; seat.carriageNum = this.carriageNum.Text; seat.carriageType = this.carriageType.Text; seat.carriageSeatNum = int.Parse(this.seatNum.Text); seat.seatType = this.seatType.Text; seat.startSite = trainNum.startSite; seat.endSite = trainNum.endSite; seat.startDate = this.dateTimePicker1.Text; if (addCarriage(carriage) > 0) { for (int i = 1; i <= int.Parse(this.seatNum.Text); i++) { if (addSeat(seat, i) > 0) { MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK); } else { MessageBox.Show("添加座位失败,请再次尝试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); deleteSeat(seat); } } } else { MessageBox.Show("添加失败,请再次尝试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private static List <TrainSeat> GetSeats(StationsDbContext context, SeatDto[] seats) { List <TrainSeat> result = new List <TrainSeat>(); foreach (var seat in seats) { var seatingClassId = context.SeatingClasses.AsNoTracking().SingleOrDefault(a => a.Name == seat.Name && a.Abbreviation == seat.Abbreviation).Id; TrainSeat trainSeat = new TrainSeat { Quantity = seat.Quantity.Value, SeatingClassId = seatingClassId, }; result.Add(trainSeat); } return(result); }
public static string ImportTrains(StationsDbContext context, string jsonString) { var trainsDto = JsonConvert.DeserializeObject <TrainDto[]>(jsonString); var sb = new StringBuilder(); var existingClassNames = context.SeatingClasses.Select(x => x.Name).ToList(); var existingClassAbbs = context.SeatingClasses.Select(x => x.Abbreviation).ToList(); var existingTrainNumbers = context.Trains.Select(x => x.TrainNumber).ToList(); var trains = new List <Train>(); foreach (var dto in trainsDto) { var flag = true; foreach (var seat in dto.Seats) { if (!existingClassAbbs.Contains(seat.Abbreviation) || !existingClassNames.Contains(seat.Name)) { flag = false; break; } } if (!flag) { sb.AppendLine(FailureMessage); continue; } if (!IsValid(dto) || !dto.Seats.All(x => IsValid(x))) { sb.AppendLine(FailureMessage); continue; } if (existingTrainNumbers.Contains(dto.TrainNumber)) { sb.AppendLine(FailureMessage); continue; } existingTrainNumbers.Add(dto.TrainNumber); var train = new Train() { TrainNumber = dto.TrainNumber, Type = dto.Type == null ? TrainType.HighSpeed : dto.Type }; foreach (var seat in dto.Seats) { var @class = context .SeatingClasses .First(x => x.Name == seat.Name && x.Abbreviation == seat.Abbreviation); var trainSeat = new TrainSeat() { SeatingClass = @class, Quantity = seat.Quantity ?? 0 }; train.TrainSeats.Add(trainSeat); } trains.Add(train); sb.AppendLine(string.Format(SuccessMessage, train.TrainNumber)); } context.Trains.AddRange(trains); context.SaveChanges(); return(sb.ToString().Trim()); }
private int deleteSeat(TrainSeat seat) { string sql = "DELETE dbo.TrainSeat WHERE trainCode='" + seat.trainCode + "' AND startDate='" + seat.startDate + "'"; return(SqlHelper.ExecuteNonQuery(sql)); }
private int addSeat(TrainSeat seat, int seatNum) { string sql = "INSERT INTO dbo.TrainSeat(trainCode,carriageNum,carriageType,carriageSeatNum,seatNum,seatType,startSite,endSite,startDate,trainType,seatState)VALUES('" + seat.trainCode + "', '" + seat.carriageNum + "', '" + seat.carriageType + "'," + seat.carriageSeatNum + ",'" + seatNum + "','" + seat.seatType + "','" + seat.startSite + "','" + seat.endSite + "','" + seat.startDate + "','" + seat.trainType + "', 1)"; return(SqlHelper.ExecuteNonQuery(sql)); }
public static string ImportTrains(StationsDbContext context, string jsonString) { var trains = JsonConvert.DeserializeObject <TrainDto[]>(jsonString); StringBuilder sb = new StringBuilder(); List <Train> validTrains = new List <Train>(); foreach (var train in trains) { if (!IsValid(train)) { sb.AppendLine(FailureMessage); continue; } if (train.Type == null) { train.Type = TrainType.HighSpeed; } List <TrainSeat> trainSeats = new List <TrainSeat>(); bool isSeatsValid = true; foreach (var seat in train.Seats) { var seatinClass = context.SeatingClasses.FirstOrDefault(x => x.Name == seat.Name && x.Abbreviation == seat.Abbreviation); if (seatinClass == null || !IsValid(seat)) { isSeatsValid = false; break; } var trainSeat = new TrainSeat { Quantity = seat.Quantity.Value, SeatingClass = seatinClass }; trainSeats.Add(trainSeat); } var isTrainNumberExist = context.Trains.Any(x => x.TrainNumber == train.TrainNumber); if (!isSeatsValid || isTrainNumberExist || validTrains.Any(x => x.TrainNumber == train.TrainNumber)) { sb.AppendLine(FailureMessage); continue; } var newTrain = new Train { TrainNumber = train.TrainNumber, Type = train.Type, TrainSeats = trainSeats }; validTrains.Add(newTrain); sb.AppendLine(string.Format(SuccessMessage, newTrain.TrainNumber)); } context.Trains.AddRange(validTrains); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportTickets(StationsDbContext context, string xmlString) { XmlSerializer serializer = new XmlSerializer(typeof(TicketDto[]), new XmlRootAttribute("Tickets")); TicketDto[] ticketDtos = (TicketDto[])serializer.Deserialize(new StringReader(xmlString)); StringBuilder sb = new StringBuilder(); List <Ticket> tickets = new List <Ticket>(); foreach (TicketDto dto in ticketDtos) { if (!IsValid(dto)) { sb.AppendLine(FailureMessage); continue; } DateTime departureTime = DateTime.ParseExact(dto.Trip.DepartureTime, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture); Trip trip = context.Trips .Include(t => t.OriginStation) .Include(t => t.DestinationStation) .Include(t => t.Train) .ThenInclude(t => t.TrainSeats) .SingleOrDefault(t => t.OriginStation.Name == dto.Trip.OriginStation && t.DestinationStation.Name == dto.Trip.DestinationStation && t.DepartureTime == departureTime); if (trip == null) { sb.AppendLine(FailureMessage); continue; } CustomerCard card = null; if (dto.Card != null) { card = context.Cards.SingleOrDefault(c => c.Name == dto.Card.Name); if (card == null) { sb.AppendLine(FailureMessage); continue; } } string seatingClassAbbreviation = dto.Seat.Substring(0, 2); int quantity = int.Parse(dto.Seat.Substring(2)); TrainSeat seatExists = trip.Train.TrainSeats.SingleOrDefault(s => s.SeatingClass.Abbreviation == seatingClassAbbreviation && quantity <= s.Quantity); if (seatExists == null) { sb.AppendLine(FailureMessage); continue; } string seat = dto.Seat; Ticket ticket = new Ticket { Trip = trip, CustomerCard = card, Price = dto.Price, SeatingPlace = seat }; tickets.Add(ticket); sb.AppendLine(string.Format("Ticket from {0} to {1} departing at {2} imported.", trip.OriginStation.Name, trip.DestinationStation.Name, trip.DepartureTime.ToString("dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture))); } context.Tickets.AddRange(tickets); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportTrains(StationsDbContext context, string jsonString) { var dtos = JsonConvert.DeserializeObject <TrainImportDto[]>(jsonString); var sb = new StringBuilder(); var trains = new List <Train>(); Func <TrainImportDto, bool> ValidateDto = new Func <TrainImportDto, bool>((dto) => { if (dto.Seats == null) { return(false); } if (dto.Seats.Select(s => s.Name).Distinct().Count() != dto.Seats.Count()) { return(false); } if (dto.Seats.Select(s => s.Quantity).Any(q => q == null || q < 0)) { return(false); } if (trains.Any(t => t.TrainNumber == dto.TrainNumber)) { return(false); } if (dto.Type == null) { dto.Type = TrainType.HighSpeed; } return(true); }); foreach (var dto in dtos) { if (!IsValid(dto) || !ValidateDto(dto)) { sb.AppendLine(FailureMessage); continue; } var hasSeat = true; var trainSeats = new List <TrainSeat>(); foreach (var trainSeatDto in dto.Seats) { var seat = context.SeatingClasses .FirstOrDefault(s => s.Name == trainSeatDto.Name && s.Abbreviation == trainSeatDto.Abbreviation); if (seat == null) { hasSeat = false; break; } var trainSeat = new TrainSeat { Quantity = trainSeatDto.Quantity ?? default(int), SeatingClassId = seat.Id }; trainSeats.Add(trainSeat); } if (!hasSeat) { sb.AppendLine(FailureMessage); continue; } var train = new Train { TrainNumber = dto.TrainNumber, Type = dto.Type, TrainSeats = trainSeats }; trains.Add(train); sb.AppendLine(String.Format(SuccessMessage, train.TrainNumber)); } context.Trains.AddRange(trains); context.SaveChanges(); var result = sb.ToString(); return(result); }
public static string ImportTrains(StationsDbContext context, string jsonString) { var trains = new List <Train>(); var result = new StringBuilder(); var objTrains = JsonConvert.DeserializeObject <TrainDto[]>(jsonString, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); foreach (var objTrain in objTrains) { var ifTrainExists = trains.Any(t => t.TrainNumber == objTrain.TrainNumber); if (!IsValid(objTrain) || ifTrainExists) { result.AppendLine(FailureMessage); continue; } var ifAllSeatsValid = true; var seats = new List <TrainSeat>(); foreach (var objSeat in objTrain.Seats) { var ifSeatClassExists = context.SeatingClasses.Any(sc => sc.Name == objSeat.Name && sc.Abbreviation == objSeat.Abbreviation); //var ifSeatClassWithSameNameDiffAbbr = context.SeatingClasses.Any(sc => (sc.Name == objSeat.Name && sc.Abbreviation != objSeat.Abbreviation) || (sc.Name != objSeat.Name && sc.Abbreviation == objSeat.Abbreviation)); if (!IsValid(objSeat) || !ifSeatClassExists) { ifAllSeatsValid = false; break; } var seat = new TrainSeat { SeatingClass = context.SeatingClasses.SingleOrDefault(sc => sc.Name == objSeat.Name && sc.Abbreviation == objSeat.Abbreviation), Quantity = objSeat.Quantity }; seats.Add(seat); } if (!ifAllSeatsValid) { result.AppendLine(FailureMessage); continue; } var train = new Train { TrainNumber = objTrain.TrainNumber, Type = Enum.Parse <TrainType>(objTrain.Type), TrainSeats = seats }; trains.Add(train); result.AppendLine(string.Format(SuccessMessage, train.TrainNumber)); } context.Trains.AddRange(trains); context.SaveChanges(); return(result.ToString()); }
public static string ImportTrains(StationsDbContext context, string jsonString) { var json = JsonConvert.DeserializeObject <List <TrainDto> >(jsonString); var sb = new StringBuilder(); foreach (var element in json) { string trainNumber = element.TrainNumber; string type = element.Type ?? "HighSpeed"; var seats = element.Seats.ToList(); var validSeats = new List <TrainSeat>(); bool ifExist = false; bool isValid = IsValid(element); foreach (var seat in seats) { string name = seat.Name; string abbreviation = seat.Abbreviation; int? quantity = seat.Quantity; bool isValidSeat = IsValid(seat); int?seatClassId = context.SeatingClasses .SingleOrDefault(s => s.Name == name && s.Abbreviation == abbreviation)?.Id; if (!context.SeatingClasses.Any(t => t.Id == seatClassId) || !isValidSeat) { ifExist = true; break; } var trainSeat = new TrainSeat() { Quantity = (int)quantity, SeatingClassId = (int)seatClassId }; validSeats.Add(trainSeat); } if (ifExist || context.Trains.Any(t => t.TrainNumber == trainNumber) || !isValid) { sb.AppendLine(FailureMessage); continue; } var train = new Train() { TrainNumber = trainNumber, TrainSeats = new List <TrainSeat>(validSeats), Type = (TrainType?)Enum.GetNames(typeof(TrainType)).ToList().IndexOf(type) }; context.Trains.Add(train); context.SaveChanges(); var msg = string.Format(SuccessMessage, trainNumber); sb.AppendLine(msg); } return(sb.ToString()); }