示例#1
0
        public IActionResult GetRejection([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var    userCp   = HttpContext.User;
            string landlord = TokenVerifier.GetLandlord(userCp);
            var    property = _context.Property.Where(p => p.ID == id).FirstOrDefault();

            if (property == null)
            {
                return(NotFound());
            }

            List <Rejection> rejection = null;

            if (TokenVerifier.CheckOfficer(userCp) || landlord == property.AppUserRef)
            {
                rejection = _context.
                            Rejection.
                            Where(p => p.ID == id).
                            OrderByDescending(p => p.Timestamp).
                            ToList();
            }
            else
            {
                return(Unauthorized());
            }

            return(Ok(rejection));
        }
示例#2
0
        public async Task <IActionResult> GetProperty([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var property = await _context.Property.FindAsync(id);

            if (property == null)
            {
                return(NotFound());
            }

            var    userCp   = HttpContext.User;
            string landlord = TokenVerifier.GetLandlord(userCp);

            if (landlord == property.AppUserRef)
            {
                return(Ok(GetMyPropertyByID(id, landlord)));
            }
            else
            {
                return(Ok(GetPropertyByID(id)));
            }
        }
示例#3
0
        public async Task <IActionResult> DeleteProperty([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var    userCp   = HttpContext.User;
            string landlord = TokenVerifier.GetLandlord(userCp);
            var    property = await _context.Property.FindAsync(id);

            if (property == null)
            {
                return(NotFound());
            }

            if (landlord == property.AppUserRef)
            {
                _context.Property.Remove(property);
                await _context.SaveChangesAsync();

                return(Ok());
            }

            return(Unauthorized());
        }
示例#4
0
        public async Task <IActionResult> PostProperty([FromBody] AddProperty addProperty)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var    userCp   = HttpContext.User;
            string landlord = TokenVerifier.GetLandlord(userCp);

            //Generate property
            Property property = _mapper.Map <AddProperty, Property>(addProperty);

            property.PropertyStatus = Property.VerificationStatus.Pending;
            property.AppUserRef     = landlord;

            //Validate property
            if (!TryValidateModel(property))
            {
                return(BadRequest());
            }

            List <BasicImage> images = new List <BasicImage>();

            int imgCount = 0;

            foreach (string imageStr in addProperty.Images)
            {
                BasicImage bi = WriteImage(imageStr, imgCount);

                if (bi != null)
                {
                    images.Add(bi);
                    imgCount++;
                }
                else
                {
                    CleanImages(images);
                    return(BadRequest());
                }
            }

            //All Images are valid and added
            _context.Property.Add(property);
            foreach (BasicImage bi in images)
            {
                bi.PropertyRef = property.ID;
                Image im = _mapper.Map <BasicImage, Image>(bi);
                _context.Image.Add(im);
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }
示例#5
0
        public IActionResult GetMyProperties()
        {
            var    userCp   = HttpContext.User;
            string landlord = TokenVerifier.GetLandlord(userCp);

            if (landlord != null)
            {
                return(Ok(GetMyProperties(landlord)));
            }
            return(NotFound());
        }
示例#6
0
        public async Task <IActionResult> UpdateProperty([FromRoute] int id, [FromBody] AddProperty addProperty)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var    userCp   = HttpContext.User;
            string landlord = TokenVerifier.GetLandlord(userCp);

            Property currentProperty = await _context.Property.FindAsync(id);

            if (currentProperty == null)
            {
                return(NotFound());
            }

            if (landlord == null || landlord != currentProperty.AppUserRef)
            {
                return(Unauthorized());
            }

            //Generate property
            Property newproperty = _mapper.Map <AddProperty, Property>(addProperty);

            newproperty.PropertyStatus = Property.VerificationStatus.Pending;
            newproperty.AppUserRef     = landlord;

            //Validate new property
            if (!TryValidateModel(newproperty))
            {
                return(BadRequest());
            }

            List <BasicImage> images = new List <BasicImage>();

            int imgCount = 0;

            foreach (string imageStr in addProperty.Images)
            {
                BasicImage bi = WriteImage(imageStr, imgCount);

                if (bi != null)
                {
                    images.Add(bi);
                    imgCount++;
                }
                else
                {
                    CleanImages(images);
                    return(BadRequest());
                }
            }

            List <Image>      oldImages      = _context.Image.Where(i => i.PropertyRef == id).ToList();
            List <BasicImage> oldBasicImages = _mapper.Map <List <Image>, List <BasicImage> >(oldImages);

            //From DB
            foreach (Image i in oldImages)
            {
                _context.Image.Remove(i);
            }

            //From disk
            CleanImages(oldBasicImages);

            currentProperty.AddressLine1        = addProperty.AddressLine1;
            currentProperty.AddressLine2        = addProperty.AddressLine2;
            currentProperty.City                = addProperty.City;
            currentProperty.County              = addProperty.County;
            currentProperty.Postcode            = addProperty.Postcode;
            currentProperty.Longitude           = addProperty.Longitude;
            currentProperty.Latitude            = addProperty.Latitude;
            currentProperty.PropertyDescription = addProperty.PropertyDescription;
            currentProperty.PropertyStatus      = Property.VerificationStatus.Pending;

            foreach (BasicImage bi in images)
            {
                bi.PropertyRef = currentProperty.ID;
                Image im = _mapper.Map <BasicImage, Image>(bi);
                _context.Image.Add(im);
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }