Пример #1
0
        public ActionResult List(Listing input)
        {
            if (ModelState.IsValid)
            {
                var status = this.propertyAdapter.UpdatePropertyListingInfo(input);

                if (status.StatusCode == 200)
                    return RedirectToAction("terms", new { id = input.BuildingId });

                HandleErrors(status);
            }

            var buildingVal = this.propertyAdapter.GetPropertyListingInfo(input.BuildingId, User.Identity.Name);

            if (buildingVal.StatusCode != 200)
                return this.NotFoundException();

            PropertyListModel model = new PropertyListModel()
            {
                Input = input,
                StepsAvailable = GetStepsAvailable(buildingVal.Result),
                UserContacts = buildingVal.Result.User.ContactInfos,
                TemporaryOrderId = buildingVal.Result.TemporaryOrderId
            };

            if (input.BuildingAmenities == null)
                input.BuildingAmenities = new List<BuildingAmenity>();

            if (input.CustomAmenities == null)
                input.CustomAmenities = new List<CustomAmenity>();

            return View(model);
        }
Пример #2
0
 public PropertyListModel()
 {
     Input = new Listing();
 }
Пример #3
0
        public PropertyListModel(Building building)
        {
            if (building.User == null)
                UserContacts = new List<ContactInfo>();
            else
                UserContacts = building.User.ContactInfos;

            Input = new Listing(building);

            TemporaryOrderId = building.TemporaryOrderId;
        }
Пример #4
0
        /// <summary>
        /// Updates the listing information specific fields on Building
        /// </summary>
        /// <param name="username">owner of the building</param>
        /// <param name="building">the building</param>
        /// <returns>
        /// Status with the updated building
        /// </returns>
        public Status<Building> UpdatePropertyListingInfo(Listing building)
        {
            var identity = CustomAuthentication.GetIdentity();

            if (!identity.IsAuthenticated)
                return Status.UnAuthorized<Building>();

            if (building == null)
                return Status.ValidationError<Building>(null, "building", "building is null");

            // normalize standard amenities
            if (building.BuildingAmenities != null)
            {
                var amenities = building.BuildingAmenities.Distinct().ToList();
                for (int x = 0; x < amenities.Count; ++x)
                {
                    if (!Amenities.Current.IsValidAmenity(amenities[x].AmenityId))
                    {
                        amenities.RemoveAt(x);
                        --x;
                    }
                }
                building.BuildingAmenities = amenities;
            }

            // normalize custom amenities
            if (building.CustomAmenities != null)
            {
                var custom = building.CustomAmenities.Distinct().ToList();
                // pascal case custom amenities
                for (int x = 0; x < custom.Count; ++x)
                {
                    if (string.IsNullOrEmpty(custom[x].Name))
                    {
                        custom.RemoveAt(x);
                        --x;
                        continue;
                    }
                    custom[x].Name = char.ToUpper(custom[x].Name[0]) + custom[x].Name.Substring(1);
                }
                building.CustomAmenities = custom;
            }

            // update it
            using (RentlerContext context = new RentlerContext())
            {
                try
                {
                    var current = (from b in context.Buildings
                                        .Include("BuildingAmenities")
                                        .Include("CustomAmenities")
                                        .Include("User.ContactInfos")
                                        .Include("ContactInfo")
                                   where b.IsDeleted == false &&
                                   b.BuildingId == building.BuildingId &&
                                   b.UserId == identity.UserId
                                   select b).SingleOrDefault();

                    if (current == null)
                        return Status.NotFound<Building>();

                    // update properties
                    current.Acres = building.Acres;
                    current.SquareFeet = building.SquareFeet;
                    current.YearBuilt = building.YearBuilt;
                    current.Bedrooms = building.Bedrooms;
                    current.Bathrooms = building.Bathrooms;

                    if (building.BuildingAmenities != null)
                    {
                        // building amenities
                        foreach (var a in building.BuildingAmenities)
                        {
                            // has this amenity already been added?
                            if (!current.BuildingAmenities.Any(ba => ba.AmenityId == a.AmenityId))
                                current.BuildingAmenities.Add(a);
                        }

                        for (int i = 0; i < current.BuildingAmenities.Count; ++i)
                        {
                            // make sure each current amenity is still wanted
                            if (!building.BuildingAmenities.Any(ba => ba.AmenityId == current.BuildingAmenities.ElementAt(i).AmenityId))
                            {
                                current.BuildingAmenities.Remove(current.BuildingAmenities.ElementAt(i));
                                --i;
                            }
                        }
                    }

                    if (building.CustomAmenities != null)
                    {
                        // custom amenities
                        foreach (var a in building.CustomAmenities)
                        {
                            // has this amenity already been added?
                            if (!current.CustomAmenities.Any(ca => ca.Name == a.Name))
                                current.CustomAmenities.Add(a);
                        }

                        for (int i = 0; i < current.CustomAmenities.Count; ++i)
                        {
                            // make sure each current amenity is still wanted
                            if (!building.CustomAmenities.Any(ca => ca.Name == current.CustomAmenities.ElementAt(i).Name))
                            {
                                current.CustomAmenities.Remove(current.CustomAmenities.ElementAt(i));
                                --i;
                            }
                        }
                    }

                    // try to make one
                    if (building.ContactInfo == null)
                        return Status.ValidationError<Building>(null, "contactinfo", "No contact information specified");

                    // validate the contactinfo
                    var contactValidation = Status.Validatate<ContactInfo>(building.ContactInfo);
                    if (contactValidation.StatusCode != 200)
                        return Status.ValidationError<Building>(null, "contactinfo", "The contact information is not valid");

                    // if the contactinfoid isn't set
                    if (building.ContactInfo.ContactInfoId == 0)
                    {
                        // add it
                        ContactInfo finalContact = building.ContactInfo;
                        current.User.ContactInfos.Add(finalContact);
                        context.SaveChanges();

                        // add it to the building
                        current.ContactInfoId = finalContact.ContactInfoId;
                    }
                    else
                    {
                        var contact = current.User.ContactInfos.Where(u => u.ContactInfoId == building.ContactInfo.ContactInfoId).FirstOrDefault();
                        if (contact == null)
                            return Status.ValidationError<Building>(null, "contactinfoid", "ContactInfoId is invalid");

                        // update the contact info
                        contact.CompanyName = building.ContactInfo.CompanyName;
                        contact.ContactInfoTypeCode = building.ContactInfo.ContactInfoTypeCode;
                        contact.Email = building.ContactInfo.Email;
                        contact.Name = building.ContactInfo.Name;
                        contact.PhoneNumber = building.ContactInfo.PhoneNumber;
                        contact.ShowEmailAddress = building.ContactInfo.ShowEmailAddress;
                        contact.ShowPhoneNumber = building.ContactInfo.ShowPhoneNumber;

                        current.ContactInfoId = contact.ContactInfoId;
                    }

                    // set defaults
                    current.UpdateDateUtc = DateTime.UtcNow;
                    current.UpdatedBy = "propertyadapter.updatepropertylistinginfo";

                    //invalidate L2 cache
                    InvalidateCache(building.BuildingId);

                    context.SaveChanges();

                    return Status.OK<Building>(current);
                }
                catch (Exception e)
                {
                    // TODO: log exception
                    return Status.Error<Building>(e.Message, null);
                }
            }
        }