コード例 #1
0
        public static string ImportProjections(CinemaContext context, string xmlString)
        {
            StringBuilder sb             = new StringBuilder();
            var           projectionsDto = XmlConverter.Deserializer <ProjectionImportModel>(xmlString, "Projections");

            foreach (var currentProjection in projectionsDto)
            {
                if (!IsValid(currentProjection) || !context.Movies.Select(x => x.Id).Contains(currentProjection.MovieId))
                {
                    sb.AppendLine("Invalid data!");
                    continue;
                }

                var projection = new Projection
                {
                    MovieId  = currentProjection.MovieId,
                    DateTime = DateTime.ParseExact(currentProjection.DateTime, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
                };

                context.Projections.Add(projection);
                context.SaveChanges();
                sb.AppendLine($"Successfully imported projection {projection.Movie.Title} on {projection.DateTime.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)}!");
            }
            return(sb.ToString().TrimEnd());
        }
コード例 #2
0
        public static string ImportProjections(CinemaContext context, string xmlString)
        {
            var root = "Projections";
            var data = XmlConverter.Deserializer <ProjectionDTO>(xmlString, root);

            List <Projection> projections = new List <Projection>();
            StringBuilder     sb          = new StringBuilder();

            foreach (var projectionDTO in data)
            {
                var projection = new Projection
                {
                    MovieId  = projectionDTO.MovieId,
                    HallId   = projectionDTO.HallId,
                    DateTime = DateTime.ParseExact(projectionDTO.DateTime, "yyyy-MM-dd H:mm:ss", CultureInfo.InvariantCulture)
                };

                var movieTitle = context.Movies.FirstOrDefault(m => m.Id == projection.MovieId);
                var hall       = context.Halls.FirstOrDefault(h => h.Id == projection.HallId);

                if (movieTitle == null || hall == null)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                projections.Add(projection);
                sb.AppendLine(String.Format(SuccessfulImportProjection, movieTitle.Title, projection.DateTime.ToString("MM/dd/yyyy")));
            }

            context.Projections.AddRange(projections);
            context.SaveChanges();

            return(sb.ToString());
        }
コード例 #3
0
        public static string ImportCustomerTickets(CinemaContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var projectionsIdInDatabase = context.Projections.Select(x => x.Id).ToList();

            var customers = new List <Customer>();

            var customerDtos = XmlConverter.Deserializer <CustomerInputDto>(xmlString, "Customers");

            foreach (var dto in customerDtos)
            {
                if (IsValid(dto) == false)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var customer = new Customer
                {
                    FirstName = dto.FirstName,
                    LastName  = dto.LastName,
                    Age       = dto.Age,
                    Balance   = dto.Balance
                };

                foreach (var currentTiket in dto.Tickets)
                {
                    if (IsValid(currentTiket) == false ||
                        projectionsIdInDatabase.Contains(currentTiket.ProjectionId) == false)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    var tiket = new Ticket
                    {
                        ProjectionId = currentTiket.ProjectionId,
                        Price        = currentTiket.Price,
                    };

                    customer.Tickets.Add(tiket);
                }

                customers.Add(customer);

                sb.AppendLine(String.Format
                                  (SuccessfulImportCustomerTicket, customer.FirstName, customer.LastName, customer.Tickets.Count));
            }

            context.AddRange(customers);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());

            throw new NotImplementedException();
        }
コード例 #4
0
        public static string ImportProjections(CinemaContext context, string xmlString)
        {
            var projectionsDtos = XmlConverter.Deserializer <ImportProjectionsDTO>(xmlString, "Projections");
            var projectionsList = new List <Projection>();
            var sb = new StringBuilder();

            foreach (var pDto in projectionsDtos)
            {
                if (!IsValid(pDto))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var validHall  = context.Halls.FirstOrDefault(x => x.Id == pDto.HallId);
                var validMovie = context.Movies.FirstOrDefault(x => x.Id == pDto.MovieId);

                if (validHall == null || validMovie == null)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                DateTime dateTime;
                var      isDateTimeValid = DateTime.TryParseExact
                                               (pDto.DateTime, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);

                if (!isDateTimeValid)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var projection = new Projection
                {
                    MovieId  = pDto.MovieId,
                    Hall     = validHall,
                    Movie    = validMovie,
                    DateTime = dateTime,
                };

                projectionsList.Add(projection);
                sb.AppendLine(
                    string.Format(SuccessfulImportProjection, validMovie.Title, dateTime.ToString("MM/dd/yyyy")));
            }

            context.Projections.AddRange(projectionsList);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
コード例 #5
0
        public static string ImportProjections(CinemaContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var moviesIdInDatabase = context.Movies.Select(x => x.Id).ToList();

            var projections = new List <Projection>();

            var projectionDtos = XmlConverter.Deserializer <ProjectionInputDto>(xmlString, "Projections");


            foreach (var dto in projectionDtos)
            {
                if (moviesIdInDatabase.Contains(dto.MovieId) == false)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                DateTime dateTime;
                var      isValidDate = DateTime.TryParseExact(dto.DateTime, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
                if (isValidDate == false)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var projection = new Projection
                {
                    MovieId  = dto.MovieId,
                    DateTime = dateTime
                };

                projections.Add(projection);

                var movieTitle = context.Movies.FirstOrDefault(x => x.Id == dto.MovieId).Title;

                var massage = String.Format(SuccessfulImportProjection, movieTitle, dateTime.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture));
                sb.AppendLine(massage);
            }

            context.AddRange(projections);
            context.SaveChanges();


            return(sb.ToString().TrimEnd());

            // throw new NotImplementedException();
        }
コード例 #6
0
        public static string ImportCustomerTickets(CinemaContext context, string xmlString)
        {
            StringBuilder   sb                = new StringBuilder();
            var             customers         = XmlConverter.Deserializer <CustomerXmlInputModel>(xmlString, "Customers");
            List <Customer> customersToImport = new List <Customer>();

            foreach (var currentCustomer in customers)
            {
                if (!IsValid(currentCustomer))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var customerToAdd = new Customer
                {
                    FirstName = currentCustomer.FirstName,
                    LastName  = currentCustomer.LastName,
                    Age       = currentCustomer.Age,
                    Balance   = currentCustomer.Balance
                };

                foreach (var currentTicket in currentCustomer.Tickets)
                {
                    var currentProjection = context.Projections.FirstOrDefault(p => p.Id == currentTicket.ProjectionId);
                    if (!IsValid(currentTicket) ||
                        currentProjection == null)
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    var ticketToAdd = new Ticket
                    {
                        Projection = currentProjection,
                        Price      = currentTicket.Price
                    };
                    customerToAdd.Tickets.Add(ticketToAdd);
                }
                customersToImport.Add(customerToAdd);
                sb.AppendLine(string.Format(SuccessfulImportCustomerTicket, customerToAdd.FirstName, customerToAdd.LastName, customerToAdd.Tickets.Count));
            }
            context.Customers.AddRange(customersToImport);
            context.SaveChanges();
            return(sb.ToString().Trim());
        }
コード例 #7
0
ファイル: Deserializer.cs プロジェクト: MilenDinev/Soft-Uni
        public static string ImportCustomerTickets(CinemaContext context, string xmlString)
        {
            StringBuilder sb           = new StringBuilder();
            var           customersDto = XmlConverter.Deserializer <CustomerTicketsImportModel>(xmlString, "Customers");

            foreach (var currentCustomer in customersDto)
            {
                if (!IsValid(currentCustomer) || !currentCustomer.Tickets.All(x => IsValid(x)))
                {
                    sb.AppendLine("Invalid data!");
                    continue;
                }

                var customer = new Customer
                {
                    FirstName = currentCustomer.FirstName,
                    LastName  = currentCustomer.LastName,
                    Age       = currentCustomer.Age,
                    Balance   = currentCustomer.Balance
                };

                foreach (var currentTicket in currentCustomer.Tickets)
                {
                    var projection = context.Projections.FirstOrDefault(x => x.Id == currentTicket.ProjectionId);

                    if (!IsValid(currentTicket) || projection == null)
                    {
                        sb.AppendLine("Invalid data!");
                        continue;
                    }

                    var ticket = new Ticket
                    {
                        Price      = currentTicket.Price,
                        Projection = projection
                    };

                    customer.Tickets.Add(ticket);
                }
                context.Customers.Add(customer);
                context.SaveChanges();
                sb.AppendLine($"Successfully imported customer {customer.FirstName} {customer.LastName} with bought tickets: {customer.Tickets.Count}!");
            }

            return(sb.ToString().TrimEnd());
        }
コード例 #8
0
        public static string ImportCustomerTickets(CinemaContext context, string xmlString)
        {
            var root = "Customers";
            var data = XmlConverter.Deserializer <CustomerDTO>(xmlString, root);

            StringBuilder   sb        = new StringBuilder();
            List <Customer> customers = new List <Customer>();

            foreach (var item in data)
            {
                if (IsValid(item))
                {
                    var customer = new Customer
                    {
                        FirstName = item.FirstName,
                        LastName  = item.LastName,
                        Age       = item.Age,
                        Balance   = item.Balance
                    };

                    foreach (var tickerDTO in item.Tickets)
                    {
                        if (IsValid(tickerDTO))
                        {
                            var ticket = new Ticket
                            {
                                CustomerId = customer.Id,
                                Price      = tickerDTO.Price
                            };

                            customer.Tickets.Add(ticket);
                        }
                    }
                    customers.Add(customer);
                    sb.AppendLine(String.Format(SuccessfulImportCustomerTicket, customer.FirstName, customer.LastName, customer.Tickets.Count));
                }
                else
                {
                    sb.AppendLine(ErrorMessage);
                }
            }

            context.Customers.AddRange(customers);
            context.SaveChanges();
            return(sb.ToString());
        }
コード例 #9
0
        public static string ImportCustomerTickets(CinemaContext context, string xmlString)
        {
            var customerTicketsDtos = XmlConverter.Deserializer <ImportCustomersDTO>(xmlString, "Customers");
            var customerList        = new List <Customer>();
            var sb = new StringBuilder();

            foreach (var ctDto in customerTicketsDtos)
            {
                if (!IsValid(ctDto))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var customer = new Customer()
                {
                    FirstName = ctDto.FirstName,
                    LastName  = ctDto.LastName,
                    Age       = ctDto.Age,
                    Balance   = ctDto.Balance,
                };

                foreach (var ticketDto in ctDto.Tickets)
                {
                    var ticket = new Ticket
                    {
                        Price        = ticketDto.Price,
                        ProjectionId = ticketDto.ProjectionId,
                    };

                    customer.Tickets.Add(ticket);
                }

                customerList.Add(customer);
                sb.AppendLine(string.Format(SuccessfulImportCustomerTicket, customer.FirstName, customer.LastName, customer.Tickets.Count()));
            }

            context.Customers.AddRange(customerList);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
コード例 #10
0
        public static string ImportProjections(CinemaContext context, string xmlString)
        {
            StringBuilder sb          = new StringBuilder();
            var           projections = XmlConverter.Deserializer <ProjectionXmlInputModel>(xmlString, "Projections");

            foreach (var currentProjection in projections)
            {
                bool isValidDateTime = DateTime.TryParseExact(
                    currentProjection.DateTime,
                    "yyyy-MM-dd HH:mm:ss",
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out DateTime currentDateTime);

                var currentMovie = context.Movies.FirstOrDefault(m => m.Id == currentProjection.MovieId);
                //var currentHall = context.Halls.FirstOrDefault(h => h.Id == currentProjection.HallId);

                if (!IsValid(currentProjection) ||
                    !isValidDateTime ||
                    currentMovie == null ||
                    !context.Halls.Any(h => h.Id == currentProjection.HallId))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var projectionToAdd = new Projection
                {
                    MovieId  = currentProjection.MovieId,
                    HallId   = currentProjection.HallId,
                    DateTime = currentDateTime
                };

                context.Projections.Add(projectionToAdd);
                context.SaveChanges();
                sb.AppendLine(string.Format(SuccessfulImportProjection, projectionToAdd.Movie.Title, projectionToAdd.DateTime.ToString("MM/dd/yyyy")));
            }

            return(sb.ToString().Trim());
        }
コード例 #11
0
        public static string ImportCustomerTickets(CinemaContext context, string xmlString)
        {
            var outputResult        = new StringBuilder();
            var customerTicketsDtos = XmlConverter.Deserializer <CustomerTicketsDto>(xmlString, "Customers");

            var validCustomersToAdd = new List <Customer>();

            foreach (var customerTicketsDto in customerTicketsDtos)
            {
                if (!IsValid(customerTicketsDto))
                {
                    outputResult.AppendLine(ErrorMessage);
                    continue;
                }
                var newCustomer = new Customer
                {
                    FirstName = customerTicketsDto.FirstName,
                    LastName  = customerTicketsDto.LastName,
                    Age       = customerTicketsDto.Age,
                    Balance   = customerTicketsDto.Balance
                };
                foreach (var ticket in customerTicketsDto.Tickets)
                {
                    newCustomer.Tickets.Add(new Ticket
                    {
                        ProjectionId = ticket.ProjectionId,
                        Price        = ticket.Price
                    });
                }
                validCustomersToAdd.Add(newCustomer);
                outputResult.AppendLine(string.Format(SuccessfulImportCustomerTicket, newCustomer.FirstName, newCustomer.LastName, newCustomer.Tickets.Count));
            }
            context.Customers.AddRange(validCustomersToAdd);
            context.SaveChanges();

            return(outputResult.ToString().TrimEnd());
        }
コード例 #12
0
        public static string ImportProjections(CinemaContext context, string xmlString)
        {
            var outputResult    = new StringBuilder();
            var projectionsDtos = XmlConverter.Deserializer <ProjectionDto>(xmlString, "Projections");

            var validProjectionsToAdd = new List <Projection>();

            foreach (var projectionDto in projectionsDtos)
            {
                var isMovieValid = context.Movies.All(x => x.Id != projectionDto.MovieId);
                var isHallValid  = context.Halls.All(x => x.Id != projectionDto.HallId);
                if (!IsValid(projectionDto) || isMovieValid || isHallValid)
                {
                    outputResult.AppendLine(ErrorMessage);
                    continue;
                }

                var newMovie = new Projection
                {
                    MovieId  = projectionDto.MovieId,
                    HallId   = projectionDto.HallId,
                    DateTime = DateTime.ParseExact(projectionDto.DateTime, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
                };
                validProjectionsToAdd.Add(newMovie);
                var targetMovieTitle = context.Movies.Where(x => x.Id == newMovie.MovieId).Select(x => x.Title).FirstOrDefault();
                outputResult.AppendLine(string.Format
                                        (
                                            SuccessfulImportProjection,
                                            targetMovieTitle,
                                            newMovie.DateTime.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture))
                                        );
            }
            context.Projections.AddRange(validProjectionsToAdd);
            context.SaveChanges();

            return(outputResult.ToString().TrimEnd());
        }