Ejemplo n.º 1
0
        public static string ImportMovies(CinemaContext context, string jsonString)
        {
            var movies = KotsevExamHelper.DeserializeObjectFromJson <List <MovieImportDto> >(jsonString)
                         .AsQueryable()
                         .ProjectTo <Movie>()
                         .ToList();

            var           mapped  = new List <Movie>();
            StringBuilder builder = new StringBuilder();

            foreach (var mv in movies)
            {
                if (KotsevExamHelper.IsValid(mv) == false)
                {
                    builder.AppendLine(ErrorMessage);
                    continue;
                }

                builder.AppendLine(string.Format(SuccessfulImportMovie, mv.Title, mv.Genre, mv.Rating));
                mapped.Add(mv);
            }

            context.Movies.AddRange(mapped);
            context.SaveChanges();

            return(builder.ToString().TrimEnd());
        }
Ejemplo n.º 2
0
        public static string ImportPrisonersMails(SoftJailDbContext context, string jsonString)
        {
            var parsed = KotsevExamHelper.DeserializeObjectFromJson <List <PrisonerImportDto> >(jsonString);
            var mapped = new List <Prisoner>();

            var builder = new StringBuilder();

            foreach (var dto in parsed)
            {
                if (!KotsevExamHelper.IsValid(dto) || dto.Mails.Any(x => KotsevExamHelper.IsValid(x) == false))
                {
                    builder.AppendLine("Invalid Data");
                    continue;
                }

                var currentPrisoner = Mapper.Map <Prisoner>(dto);
                mapped.Add(currentPrisoner);

                builder.AppendLine($"Imported {currentPrisoner.FullName} {currentPrisoner.Age} years old");
            }

            context.Prisoners.AddRange(mapped);
            context.SaveChanges();

            return(builder.ToString().TrimEnd());
        }
Ejemplo n.º 3
0
        public static string ExportTopMovies(CinemaContext context, int rating)
        {
            var movies = context.Movies
                         .Where(x => x.Rating >= rating && x.Projections.Select(z => z.Tickets).Any())
                         .Take(10)
                         .OrderByDescending(x => x.Rating)
                         .ThenByDescending(x => x.Projections.Sum(z => z.Tickets.Sum(w => w.Price)))
                         //.ProjectTo<MovieExportDto>()
                         .Select(x => new
            {
                MovieName    = x.Title,
                Rating       = $"{x.Rating:F2}",
                TotalIncomes = $"{x.Projections.Sum(z => z.Tickets.Sum(w => w.Price)):F2}",
                Customers    = x.Projections
                               .SelectMany(z => z.Tickets).Select(w => new
                {
                    FirstName = w.Customer.FirstName,
                    LastName  = w.Customer.LastName,
                    Balance   = $"{w.Customer.Balance:F2}"
                })
                               .OrderByDescending(w => w.Balance)
                               .ThenBy(w => w.FirstName)
                               .ThenBy(w => w.LastName)
                               .ToArray()
            })
                         .ToList();

            var json = KotsevExamHelper.SerializeObjectToJson(movies);

            return(json);
        }
Ejemplo n.º 4
0
        public static string ExportTopCustomers(CinemaContext context, int age)
        {
            var customers = context.Customers
                            .Where(x => x.Age >= age)
                            .OrderByDescending(x => x.Tickets.Sum(y => y.Price))
                            .Take(10)
                            .ProjectTo <CustomerExportDto>()
                            .ToList();

            var xml = KotsevExamHelper.SerializeObjectToXml <List <CustomerExportDto> >(customers, "Customers");

            return(xml);
        }
Ejemplo n.º 5
0
        public static string ImportProjections(CinemaContext context, string xmlString)
        {
            var doc = XDocument.Parse(xmlString)
                      .Root
                      .Elements()
                      .ToList();

            StringBuilder builder = new StringBuilder();
            var           mapped  = new List <Projection>();

            foreach (var elProj in doc)
            {
                var movieId = int.Parse(elProj.Element("MovieId").Value);
                var movie   = KotsevExamHelper.GetObjectFromSet <Movie, CinemaContext>(x => x.Id == movieId, context);

                if (movie == null)
                {
                    builder.AppendLine(ErrorMessage);
                    continue;
                }

                var hallId = int.Parse(elProj.Element("HallId").Value);
                var hall   = KotsevExamHelper.GetObjectFromSet <Hall, CinemaContext>(x => x.Id == hallId, context);

                if (hall == null)
                {
                    builder.AppendLine(ErrorMessage);
                    continue;
                }

                var projDate = DateTime.ParseExact(elProj.Element("DateTime").Value, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

                var currentProjection = new Projection()
                {
                    Movie    = movie,
                    Hall     = hall,
                    DateTime = projDate
                };

                mapped.Add(currentProjection);
                builder.AppendLine(string.Format(SuccessfulImportProjection, movie.Title, projDate.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)));
            }

            context.Projections.AddRange(mapped);
            context.SaveChanges();
            return(builder.ToString().TrimEnd());
        }
Ejemplo n.º 6
0
        public static string ImportHallSeats(CinemaContext context, string jsonString)
        {
            var hallSeats = KotsevExamHelper.DeserializeObjectFromJson <List <HallImportDto> >(jsonString);

            StringBuilder builder = new StringBuilder();

            var mapped = new List <Hall>();

            foreach (var hs in hallSeats)
            {
                if (KotsevExamHelper.IsValid(hs) == false)
                {
                    builder.AppendLine(ErrorMessage);
                    continue;
                }

                var currentHall = Mapper.Map <Hall>(hs);

                for (int i = 1; i <= hs.Seats; i++)
                {
                    var currentSeat = new Seat()
                    {
                        Hall = currentHall,
                    };

                    currentHall.Seats.Add(currentSeat);
                }

                mapped.Add(currentHall);
                string type = currentHall.Is4Dx && currentHall.Is3D ? "4Dx/3D" : !currentHall.Is3D && !currentHall.Is4Dx ? "Normal" : currentHall.Is3D ? "3D" : "4Dx";

                builder.AppendLine(string.Format(SuccessfulImportHallSeat, currentHall.Name, type, currentHall.Seats.Count));
            }

            context.Halls.AddRange(mapped);
            context.SaveChanges();

            return(builder.ToString().TrimEnd());
        }
Ejemplo n.º 7
0
        public static string ImportDepartmentsCells(SoftJailDbContext context, string jsonString)
        {
            var parsed = KotsevExamHelper.DeserializeObjectFromJson <DepartmentImportDto[]>(jsonString)
                         .ToList();

            StringBuilder builder = new StringBuilder();

            var mapped = new List <Department>();

            foreach (var x in parsed)
            {
                if (!KotsevExamHelper.IsValid(x) || x.Cells.Any(y => KotsevExamHelper.IsValid(y) == false))
                {
                    builder.AppendLine("Invalid Data");
                    continue;
                }

                var department = Mapper.Map <Department>(x);

                //var cells = x.Cells
                //    .Distinct()
                //    .AsQueryable()
                //    .ProjectTo<Cell>(new { currentDepartment = department })
                //    .ToList();

                //cells.ForEach(c => department.Cells.Add(c));

                mapped.Add(department);
                builder.AppendLine($"Imported {department.Name} with {department.Cells.Count} cells");
            }

            context.Departments.AddRange(mapped);
            context.SaveChanges();

            return(builder.ToString().TrimEnd());
        }
Ejemplo n.º 8
0
        public static string ImportOfficersPrisoners(SoftJailDbContext context, string xmlString)
        {
            var doc = XDocument.Parse(xmlString);

            var elements = doc.Root
                           .Elements()
                           .ToList();

            var builder = new StringBuilder();
            var mapped  = new List <Officer>();

            foreach (var element in elements)
            {
                var currentOfficer = new Officer()
                {
                    FullName = element.Element("Name").Value ?? null,
                    Salary   = decimal.Parse(element.Element("Money").Value ?? "-0.01"),
                };

                Weapon weaponType;
                bool   isWeaponParseSuccessfull = Enum.TryParse <Weapon>(element.Element("Weapon").Value, out weaponType);

                Position positionType;
                bool     isPositionParseSuccessfull = Enum.TryParse <Position>(element.Element("Position").Value, out positionType);

                if (currentOfficer.FullName == null || currentOfficer.Salary < 0 ||
                    !isWeaponParseSuccessfull || !isPositionParseSuccessfull)
                {
                    builder.AppendLine("Invalid Data");
                    continue;
                }

                currentOfficer.Position = positionType;
                currentOfficer.Weapon   = weaponType;

                var desiredDepartmentId = int.Parse(element.Element("DepartmentId").Value);

                var department = KotsevExamHelper.GetObjectFromSet <Department, SoftJailDbContext>(x => x.Id == desiredDepartmentId, context);

                //if (department == null)
                //{
                //    builder.AppendLine("Invalid Data");
                //    continue;
                //}

                //currentOfficer.Department = department;

                currentOfficer.DepartmentId = desiredDepartmentId;

                foreach (var prisoner in element.Element("Prisoners").Elements().ToList())
                {
                    var id = int.Parse(prisoner.Attribute("id").Value ?? "-1");

                    if (id < 0 || currentOfficer.OfficerPrisoners.Any(x => x.PrisonerId == id))
                    {
                        continue;
                    }

                    //var prisonerInstance = KotsevExamHelper.GetObjectFromSet<Prisoner, SoftJailDbContext>(x => x.Id == id, context);

                    //if (prisonerInstance == null)
                    //continue;

                    currentOfficer.OfficerPrisoners.Add(new OfficerPrisoner {
                        PrisonerId = id, Officer = currentOfficer
                    });
                }

                mapped.Add(currentOfficer);
                builder.AppendLine($"Imported {currentOfficer.FullName} ({currentOfficer.OfficerPrisoners.Count} prisoners)");
            }

            //var count = mapped.SelectMany(x => x.OfficerPrisoners).Count();
            context.Officers.AddRange(mapped);
            context.SaveChanges();

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