Example #1
0
        public Status <PropertySearch> SearchForUserProperty(PropertySearch search)
        {
            var ident = CustomAuthentication.GetIdentity();

            if (!ident.IsAuthenticated)
            {
                return(Status.UnAuthorized <PropertySearch>());
            }

            // if it is null create a new one
            if (search == null)
            {
                search = new PropertySearch();
            }
            if (search.Page < 1)
            {
                search.Page = 1;
            }
            if (search.ResultsPerPage < 5)
            {
                search.ResultsPerPage = 25;
            }
            if (string.IsNullOrEmpty(search.OrderBy))
            {
                search.OrderBy = "CreateDate";
            }

            using (var data = this.service.Get())
            {
                var result = data.Building.SearchUserBuildings(ident.UserId, search);
                search.Results = result;
                return(Status.OK <PropertySearch>(search));
            }
        }
Example #2
0
        public Status <bool> DeleteProperty(long buildingId)
        {
            var ident = CustomAuthentication.GetIdentity();

            if (!ident.IsAuthenticated)
            {
                return(Status.UnAuthorized <bool>());
            }

            using (var data = this.service.Get())
            {
                // get the building and make sure the user owns it
                var preview = data.Building.GetBuildingPreviewById(buildingId);

                if (preview == null)
                {
                    return(Status.NotFound <bool>());
                }
                if (preview.UserId != ident.UserId)
                {
                    return(Status.UnAuthorized <bool>());
                }

                // delete the building
                data.Building.DeleteBuilding(buildingId);

                data.Save();

                return(Status.OK <bool>(true));
            }
        }
Example #3
0
        public Status <Listing> ManageListingById(long listingId)
        {
            var ident = CustomAuthentication.GetIdentity();

            if (!ident.IsAuthenticated)
            {
                return(Status.UnAuthorized <Listing>());
            }

            using (var data = this.service.Get())
            {
                var result = data.Listing.GetListingById(listingId);

                if (result == null)
                {
                    return(Status.NotFound <Listing>());
                }

                if (result.UserId == ident.UserId)
                {
                    result.IsOwnedByCurrentUser = true;
                }

                if (!result.IsOwnedByCurrentUser)
                {
                    return(Status.UnAuthorized <Listing>());
                }

                // get the stats
                var connection = ConnectionGateway.Current.GetReadConnection();
                try
                {
                    var listingViewTask = connection.Hashes.GetString(App.RedisDatabase,
                                                                      CacheKeys.LISTING_VIEWS, listingId.ToString());
                    string listingViewResult = connection.Wait(listingViewTask);
                    if (string.IsNullOrEmpty(listingViewResult))
                    {
                        result.PageViews = 0;
                    }
                    result.PageViews = long.Parse(listingViewResult);

                    var listingSearchTask = connection.Hashes.GetString(App.RedisDatabase,
                                                                        CacheKeys.LISTING_SEARCH_VIEWS, listingId.ToString());
                    string listingSearchResult = connection.Wait(listingSearchTask);
                    if (string.IsNullOrEmpty(listingSearchResult))
                    {
                        result.SearchViews = 0;
                    }
                    result.SearchViews = long.Parse(listingSearchResult);
                }
                catch (Exception)
                {
                    result.PageViews   = 0;
                    result.SearchViews = 0;
                }

                return(Status.OK <Listing>(result));
            }
        }
        public void GetIdentityUnauthenticatedUser()
        {
            var identity = CustomAuthentication.GetIdentity();

            Assert.IsNotNull(identity);
            Assert.AreEqual(identity.IsAuthenticated, false);
            Assert.AreEqual(identity.Username, string.Empty);
            Assert.AreEqual(identity.UserId, 0);
        }
        /// <summary>
        /// Gets the saved listings for user.
        /// </summary>
        /// <param name="username">The username to get the saved listings for.</param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns>
        /// A list of saved listings for a user.
        /// </returns>
        public Status <PaginatedList <BuildingPreview> > GetFavoritesForUser(
            string username, int?pageNumber, int?pageSize)
        {
            var identity = CustomAuthentication.GetIdentity();

            if (!identity.IsAuthenticated)
            {
                return(Status.UnAuthorized <PaginatedList <BuildingPreview> >());
            }

            if (!pageNumber.HasValue)
            {
                pageNumber = 0;
            }
            if (!pageSize.HasValue || pageSize.Value > 100)
            {
                pageSize = 25;
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                return(Status.ValidationError <PaginatedList <BuildingPreview> >(null, "username", "username is required"));
            }

            using (var context = new RentlerContext())
            {
                var props = (from sb in context.SavedBuildings
                             join b in context.Buildings on sb.BuildingId equals b.BuildingId
                             where sb.UserId == identity.UserId &&
                             b.IsActive == true &&
                             b.IsRemovedByAdmin == false
                             orderby b.CreateDateUtc descending
                             select b).ToList();

                var queryableProps = props.Select(b => new BuildingPreview()
                {
                    Bathrooms             = b.Bathrooms.Value,
                    Bedrooms              = b.Bedrooms.Value,
                    BuildingId            = b.BuildingId,
                    City                  = b.City,
                    IsFeatured            = false,
                    Price                 = b.Price,
                    PrimaryPhotoExtension = b.PrimaryPhotoExtension,
                    PrimaryPhotoId        = b.PrimaryPhotoId,
                    State                 = b.State,
                    Title                 = string.IsNullOrWhiteSpace(b.Title) ? b.Address1 : b.Title,
                    Address1              = b.Address1
                }).AsQueryable <BuildingPreview>();

                return(Status.OK <PaginatedList <BuildingPreview> >(
                           new PaginatedList <BuildingPreview>(queryableProps, pageNumber.Value, pageSize.Value)));
            }
        }
 public void GetIdentityInvalidIdentity()
 {
     HttpContext.Current.User = new GenericPrincipal(
         new FakeIdentity("cyberkruz"), new string[0]);
     try
     {
         CustomAuthentication.GetIdentity();
     }
     catch (InvalidCastException)
     {
         Assert.IsTrue(true);
     }
 }
        public void GetIdentityIsAuthenticated()
        {
            // unauthenticated user
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1, "cyberkruz", DateTime.Now, DateTime.Now.AddDays(30),
                true, "4", FormsAuthentication.FormsCookiePath);
            FormsIdentity ident = new FormsIdentity(ticket);

            HttpContext.Current.User = new GenericPrincipal(ident, new string[0]);

            var identity = CustomAuthentication.GetIdentity();

            Assert.IsTrue(identity.IsAuthenticated);
            Assert.AreEqual(identity.Username, "cyberkruz");
            Assert.AreEqual(identity.UserId, 4);
        }
        /// <summary>
        /// Removes a Saved Listing for a particular User
        /// </summary>
        /// <param name="listingId">listing identifier</param>
        /// <param name="username">user identifier</param>
        /// <returns>
        /// A status with the saved building
        /// </returns>
        public Status <bool> DeleteSavedBuilding(long listingId, string username)
        {
            var identity = CustomAuthentication.GetIdentity();

            if (!identity.IsAuthenticated)
            {
                return(Status.UnAuthorized <bool>());
            }

            if (listingId == 0)
            {
                return(Status.ValidationError <bool>(false, "listingId", "listingId is required"));
            }

            if (string.IsNullOrWhiteSpace(username))
            {
                return(Status.ValidationError <bool>(false, "username", "username is required"));
            }

            using (var context = new RentlerContext())
            {
                try
                {
                    SavedBuilding save = (from s in context.SavedBuildings
                                          where s.BuildingId == listingId &&
                                          s.UserId == identity.UserId
                                          select s).SingleOrDefault();

                    if (save == null)
                    {
                        return(Status.NotFound <bool>());
                    }

                    context.SavedBuildings.Remove(save);
                    context.SaveChanges();

                    InvalidateCache(save.BuildingId);

                    return(Status.OK <bool>(true));
                }
                catch (Exception ex)
                {
                    return(Status.Error <bool>(ex.Message, false));
                }
            }
        }
Example #9
0
        public Status <string[]> GetOrderedPhotoIds(long buildingId)
        {
            var identity = CustomAuthentication.GetIdentity();

            if (!identity.IsAuthenticated)
            {
                return(Status.UnAuthorized <string[]>());
            }

            if (buildingId == 0)
            {
                return(Status.ValidationError <string[]>(null, "buildingId", "buildingId cannot be empty"));
            }

            using (RentlerContext context = new RentlerContext())
            {
                try
                {
                    var building = (from b in context.Buildings.Include("Photos")
                                    where b.BuildingId == buildingId
                                    select b).SingleOrDefault();

                    if (building == null)
                    {
                        return(Status.NotFound <string[]>());
                    }

                    var photoIds = building.Photos
                                   .OrderBy(p => p.SortOrder)
                                   .Select(p => p.PhotoId.ToString())
                                   .ToArray();

                    return(Status.OK <string[]>(photoIds));
                }
                catch (Exception ex)
                {
                    return(Status.Error <string[]>(ex.Message, null));
                }
            }
        }
Example #10
0
        /// <summary>
        /// Sets the application for user.
        /// </summary>
        /// <param name="username">The username of the user to set the application for.</param>
        /// <param name="userApplication">The user's application.</param>
        /// <returns>
        /// The user application that was saved.
        /// </returns>
        public Status <UserApplication> SaveApplicationForUser(
            string username, UserApplication userApplication)
        {
            var identity = CustomAuthentication.GetIdentity();

            if (!identity.IsAuthenticated)
            {
                return(Status.UnAuthorized <UserApplication>());
            }

            using (var context = new RentlerContext())
            {
                try
                {
                    bool isNew = false;

                    var application = (from u in context.UserApplications
                                       where u.UserId == identity.UserId
                                       select u).SingleOrDefault();

                    if (application == null)
                    {
                        application = new UserApplication {
                            UserId = identity.UserId
                        };
                        isNew = true;
                    }

                    application.ConvictedExplaination        = userApplication.ConvictedExplaination;
                    application.EmergencyContact             = userApplication.EmergencyContact;
                    application.EmergencyContactAddressLine1 = userApplication.EmergencyContactAddressLine1;
                    application.EmergencyContactAddressLine2 = userApplication.EmergencyContactAddressLine2;
                    application.EmergencyContactCity         = userApplication.EmergencyContactCity;
                    application.EmergencyContactPhone        = userApplication.EmergencyContactPhone;
                    application.EmergencyContactState        = userApplication.EmergencyContactState;
                    application.EmergencyContactZip          = userApplication.EmergencyContactZip;
                    application.FirstName                   = userApplication.FirstName;
                    application.HasBeenConvicted            = userApplication.HasBeenConvicted;
                    application.HasEverBeenUnlawfulDetainer = userApplication.HasEverBeenUnlawfulDetainer;
                    application.LastName              = userApplication.LastName;
                    application.PresentAddressLine1   = userApplication.PresentAddressLine1;
                    application.PresentAddressLine2   = userApplication.PresentAddressLine2;
                    application.PresentCity           = userApplication.PresentCity;
                    application.PresentEmployer       = userApplication.PresentEmployer;
                    application.PresentEmployerPhone  = userApplication.PresentEmployerPhone;
                    application.PresentLandlord       = userApplication.PresentLandlord;
                    application.PresentLandlordPhone  = userApplication.PresentLandlordPhone;
                    application.PresentPhone          = userApplication.PresentPhone;
                    application.PresentState          = userApplication.PresentState;
                    application.PresentZip            = userApplication.PresentZip;
                    application.PreviousAddressLine1  = userApplication.PreviousAddressLine1;
                    application.PreviousAddressLine2  = userApplication.PreviousAddressLine2;
                    application.PreviousCity          = userApplication.PreviousCity;
                    application.PreviousEmployer      = userApplication.PreviousEmployer;
                    application.PreviousEmployerPhone = userApplication.PreviousEmployerPhone;
                    application.PreviousLandlord      = userApplication.PreviousLandlord;
                    application.PreviousLandlordPhone = userApplication.PreviousLandlordPhone;
                    application.PreviousState         = userApplication.PreviousState;
                    application.PreviousZip           = userApplication.PreviousZip;
                    application.Ssn                  = userApplication.Ssn;
                    application.UpdateDateUtc        = DateTime.UtcNow;
                    application.UpdatedBy            = "accountadapter";
                    application.VehicleColor         = userApplication.VehicleColor;
                    application.VehicleLicenseNumber = userApplication.VehicleLicenseNumber;
                    application.VehicleMake          = userApplication.VehicleMake;
                    application.VehicleModel         = userApplication.VehicleModel;
                    application.VehicleState         = userApplication.VehicleState;
                    application.VehicleYear          = userApplication.VehicleYear;

                    // new applications need to be added to the context
                    if (isNew)
                    {
                        context.UserApplications.Add(application);
                    }

                    context.SaveChanges();

                    return(Status.OK <UserApplication>(application));
                }
                catch (Exception ex)
                {
                    // TODO: log exception
                    return(Status.Error <UserApplication>("System was unable to create/update application", null));
                }
            }
        }