Ejemplo n.º 1
0
        public void AddApartmentPhoto(Apartment apartment, ApartmentPhoto photo)
        {
            Apartment Apartment = context
                .Apartments
                .Include(m => m.ApartmentPhotos)
                .Include(m => m.DefaultPhoto)
                .Where(m => m.ApartmentId == apartment.ApartmentId)
                .FirstOrDefault();
            Apartment.ApartmentPhotos.Add(photo);

            context.SaveChanges();
            if (Apartment.DefaultPhoto == null)
            {
                SetApartmentDefaultPhoto(Apartment, photo);
            }
        }
Ejemplo n.º 2
0
 public void SetApartmentDefaultPhoto(Apartment apartment, ApartmentPhoto photo)
 {
     Apartment Apartment = context
         .Apartments
         .Include(m => m.ApartmentPhotos)
         .Where(m => m.ApartmentId == apartment.ApartmentId)
         .FirstOrDefault();
     Apartment.DefaultPhoto = photo;
     context.SaveChanges();
 }
Ejemplo n.º 3
0
        public ActionResult EditApartmentPhoto(EditApartmentPhotoViewResult newApartment)
        {
            if (ModelState.IsValid)
            {
                if (newApartment.InputImages != null)
                {
                    Apartment apartment = ApartmentRepository.GetApartment(newApartment.ApartmentId);
                    var Photos = ApartmentRepository.GetApartmentPhotos(apartment);
                    var HasDafaultPhoto = true;
                    if (Photos.Count==0)
                    {
                        HasDafaultPhoto = false;
                    }
                    foreach (var photo in newApartment.InputImages.Where(m => m != null))
                    {
                        int height, width, max;
                        int centerx, centery;
                        int offsetx, offsety;
                        using (System.Drawing.Image image = System.Drawing.Image.FromStream(photo.InputStream, true, true))
                        {
                            height = image.Height;
                            width = image.Width;
                            centerx = width / 2;
                            centery = height / 2;
                            if (height > width)
                            {
                                max = width;
                                offsetx = 0;
                                offsety = centery - max / 2;
                            }
                            else
                            {
                                max = height;
                                offsety = 0;
                                offsetx = centerx - max / 2;
                            }

                        }
                        ApartmentPhoto Photo = new ApartmentPhoto { ApartmentId = newApartment.ApartmentId, Links = new List<ApartmentPhotoLink>() };
                        Photo.Links.Add(new ApartmentPhotoLink { Link = PreviewCreator.SaveImage(photo.InputStream), TypeId = 1 });
                        Photo.Links.Add(new ApartmentPhotoLink { Link = PreviewCreator.SaveImageAndResize(photo.InputStream, (int)offsetx, (int)offsety, (int)max, (int)max, new System.Drawing.Size(550, 550)), TypeId = 2 });
                        Photo.Links.Add(new ApartmentPhotoLink { Link = PreviewCreator.SaveImageAndResize(photo.InputStream, (int)offsetx, (int)offsety, (int)max, (int)max, new System.Drawing.Size(100, 100)), TypeId = 3 });

                        ApartmentRepository.AddApartmentPhoto(apartment, Photo);
                        if (!HasDafaultPhoto)
                        {
                            ApartmentRepository.SetApartmentDefaultPhoto(apartment, Photo);
                        }
                        HasDafaultPhoto = true;
                        TempData["toastrMessage"] = String.Format("Квартира изменена");
                        TempData["toastrType"] = "success";
                    }
                }

            }
            else
            {
            }

            return RedirectToAction("EditApartmentPhoto");
        }