Ejemplo n.º 1
0
        public async Task <IActionResult> AddHome(AddHomeViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await userManager.GetUserAsync(HttpContext.User);

                Home home = new Home
                {
                    Name             = model.Name,
                    NumberOfFloors   = model.NumberOfFloors,
                    NumberOfRooms    = model.NumberOfRooms,
                    PropertyType     = PropertyType.Dom,
                    Address          = model.Address,
                    Advance          = model.Advance,
                    City             = model.City,
                    ConstructionYear = model.ConstructionYear,
                    FullDescription  = model.Description,
                    HaveFurnishings  = model.HaveFurnishings,
                    HaveGarage       = model.HaveGarage,
                    SquareMetrage    = model.SquareMetrage,
                    TotalArea        = model.TotalArea,
                    Price            = model.Price,
                    MainPageDisplay  = model.MainPageDisplay,
                    Images           = new List <Photo>(),
                    OwnerID          = user.Id,
                    AgencyName       = model.AgencyName
                };

                Advertisement adv = new Advertisement
                {
                    PropertyType = PropertyType.Dom,
                    DateAdded    = DateTime.Now,
                    EndDate      = adManager.SetExpirationDate(model.AdvLength),
                    Name         = model.Name,
                    User         = user,
                    Home         = home
                };
                string mainName;
                if (model.MainImage != null)
                {
                    mainName = adManager.ReturnUniqueName(model.MainImage);
                    var photo = new Photo()
                    {
                        PhotoName = mainName,
                        PhotoPath = Path.Combine(hostingEnvironment.WebRootPath, "images")
                                    //If using HasOne (OneToOne Relationship) instead of Owns One(OwnedType)
                                    //HomePrincipal = home
                    };
                    adManager.UploadPhoto(model.MainImage, Path.Combine(hostingEnvironment.WebRootPath, "images"), mainName);
                    home.MainImageName = photo.PhotoName;
                }
                //Lista przchowująca dane nazw zdjęć ponieważ uniqueName ma ograniczony scope
                List <string> photoNames = new List <string>();
                if (model.Images != null && model.Images.Count > 0)
                {
                    //Tworzenie Folderu na zdjęcia
                    var galleryPath = Path.Combine(hostingEnvironment.WebRootPath, "images", "Gallery_home_id_" + $"{model.Name}");
                    if (!Directory.Exists(galleryPath))
                    {
                        Directory.CreateDirectory(galleryPath);
                    }
                    foreach (var photo in model.Images)
                    {
                        string uniqueName = adManager.ReturnUniqueName(photo);
                        photoNames.Add(uniqueName);
                        adManager.UploadPhoto(photo, galleryPath, uniqueName);
                    }
                    foreach (var photoName in photoNames)
                    {
                        home.Images.Add(new Photo {
                            PhotoName = photoName, PhotoPath = galleryPath                         /*HomesPrincipal = home*/
                        });
                    }
                }
                adv.Home = home;
                unit.AdvertismentRepository.AddAdvertisement(adv);
                unit.SaveData();
                //Jeśli ogłoszenie ma być wyświetlane na stronie głównej,
                //następuje przekierowanie do metody Promote gdzie trzeba dokonać płatności za promowanie
                if (home.MainPageDisplay == true)
                {
                    return(RedirectToAction("Promote", "Advertisements", new { id = adv.AdvertisementId }));
                }
            }
            return(RedirectToAction("Success", "Customers"));
        }