Beispiel #1
0
        public static string ImportProjections(CinemaContext context, string xmlString)
        {
            StringBuilder     sb                  = new StringBuilder();
            var               projections         = XmlConverter.Deserializer <ProjectionXmlInputModel>(xmlString, "Projections");
            List <Projection> projectionsToImport = new List <Projection>();

            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);

                if (!IsValid(currentProjection) ||
                    !isValidDateTime ||
                    currentMovie == null)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

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

                projectionsToImport.Add(projectionToAdd);
                sb.AppendLine(string.Format(SuccessfulImportProjection, currentMovie.Title, projectionToAdd.DateTime.ToString("MM/dd/yyyy")));
            }
            context.Projections.AddRange(projectionsToImport);
            context.SaveChanges();
            return(sb.ToString().Trim());
        }
Beispiel #2
0
        public static string ExportTopCustomers(CinemaContext context, int age)
        {
            var customers = context
                            .Customers
                            .Where(a => a.Age >= age)
                            .ToArray()
                            .OrderByDescending(x => x.Tickets.Sum(t => t.Price))
                            .Select(x => new CustomerExportDto
            {
                FirstName  = x.FirstName,
                LastName   = x.LastName,
                SpentMoney = x.Tickets.Sum(t => t.Price).ToString("f2"),
                SpentTime  = TimeSpan.FromSeconds(x.Tickets.Sum(p => p.Projection.Movie.Duration.TotalSeconds)).ToString(@"hh\:mm\:ss")
            })
                            .Take(10)
                            .ToArray();

            var xmlString = XmlConverter.Serialize(customers, "Customers");

            return(xmlString);

            // throw new NotImplementedException();
        }
Beispiel #3
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());
        }
Beispiel #4
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());
        }