// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, DbInfoContext context)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler();
            }

            context.EnsureSeedDataForContext();
            app.UseStatusCodePages();
            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Entities.Apartment, Models.ApartmentDto>();
                // map to Buyer with all information (including list of apartments owned)
                cfg.CreateMap <Entities.Buyer, Models.BuyerDto>();
                // map to Buyer with minimum information (without the apartments owned)
                cfg.CreateMap <Entities.Buyer, Models.BuyerWithoutApartmentsDto>()
                .ForMember(dest => dest.NbOfOwnedApartments, opt => opt.MapFrom(src => src.OwnedApartments.Count));
                // the price of an apartments is always NbOfRooms * 3000 (assignement 1)
                cfg.CreateMap <Models.ApartmentForCreationDto, Entities.Apartment>()
                .ForMember(dest => dest.Price, opt => opt.MapFrom(src => src.NbOfRooms * 3000));
                // mapper for the update of an apartment
                cfg.CreateMap <Models.ApartmentForUpdateDto, Entities.Apartment>();
                // mapper for the partial update of an apartment
                cfg.CreateMap <Entities.Apartment, Models.ApartmentForUpdateDto>();
            });
            app.UseCors("CorsPolicy");
            app.UseMvc();
        }
        public static void EnsureSeedDataForContext(this DbInfoContext context)
        {
            if (context.Apartments.Any())
            {
                return;
            }

            var apartments = new List <Apartment>()
            {
                new Apartment()
                {
                    Title     = "Zouzou Sky",
                    Address   = "Tripoli",
                    NbOfRooms = 2,
                    Price     = 5000
                },
                new Apartment()
                {
                    Title     = "Georges Bldg.",
                    Address   = "Beirut, Achrafieh",
                    NbOfRooms = 3,
                    Price     = 10000
                },
                new Apartment()
                {
                    Title     = "Sama Beirut Apartment 4",
                    Address   = "Beirut, Achrafieh",
                    NbOfRooms = 8,
                    Price     = 50000
                },
                new Apartment()
                {
                    Title     = "Cloud 9 Tower",
                    Address   = "Beirut, Baabda",
                    NbOfRooms = 6,
                    Price     = 25000
                },
                new Apartment()
                {
                    Title     = "Sky Hotel",
                    Address   = "Baabda",
                    NbOfRooms = 4,
                    Price     = 10000
                }
            };

            var buyers = new List <Buyer>()
            {
                new Buyer
                {
                    FullName = "John Smith",
                    Credits  = 10000,
                },
                new Buyer
                {
                    FullName = "William Jones",
                    Credits  = 100000,
                },
                new Buyer
                {
                    FullName = "Gordon Freeman",
                    Credits  = 2000000,
                }
            };

            context.Apartments.AddRange(apartments);
            context.Buyers.AddRange(buyers);
            context.SaveChanges();
        }
Exemple #3
0
 public BuyerInfoRepository(DbInfoContext context)
 {
     _context = context;
 }
 public ApartmentInfoRepository(DbInfoContext context)
 {
     _context = context;
 }