[TestMethod] //[2] | Update Property
        public void PutPropertyUpdatesProperty()
        {
            //Arrange
            var propertiesController = new PropertiesController();

            var newProp = new PropertyModel
            {
                Name = "Wonder Mansion",
                Address1 = "122 Wonka Way",
                Address2 = "",
                City = "Golden Coast",
                Zip = "23123",
                State = "CA"
               
            };

            
            //The result of the PostRequest
            IHttpActionResult result = propertiesController.PostProperty( newProp);

            //Cast result as the content result so I can gather information from Content Result
            CreatedAtRouteNegotiatedContentResult<PropertyModel> contentResult = (CreatedAtRouteNegotiatedContentResult<PropertyModel>)result;

            //REsult containts the property I had just created
            result = propertiesController.GetProperty(contentResult.Content.PropertyId);

            //GET PropertyModel from Result
            OkNegotiatedContentResult<PropertyModel> propertyResult = (OkNegotiatedContentResult<PropertyModel>)result;

            //Act
            result = propertiesController.PutProperty(propertyResult.Content.PropertyId, newProp);

            //Assert
            Assert.IsInstanceOfType(result, typeof(StatusCodeResult));
        }
        public void DeletePropertyDeletesProperty()
        {
            //Arrange:
            // Instantiate PropertiesController so its methods can be called
            // Create a new property to be deleted, and get its property ID

            var propertyController = new PropertiesController();

            var property = new PropertyModel
            {
                Name = "Office Space",
                Address1 = "101 Broadway",
                City = "San Francisco",
                State = "CA"
            };
            IHttpActionResult propertyResult = propertyController.PostProperty(property);
            CreatedAtRouteNegotiatedContentResult<PropertyModel> contentResult =
                (CreatedAtRouteNegotiatedContentResult<PropertyModel>)propertyResult;

            int propertyIdToDelete = contentResult.Content.PropertyId;

            // Add a lease corresponding to the property
            int createdLeaseId;
            using (var leaseController = new LeasesController())
            {
                var lease = new LeaseModel
                {
                    CreatedDate = new DateTime(2014, 9, 30),
                    PropertyId = propertyIdToDelete,
                    TenantId = 1,
                    StartDate = new DateTime(2015, 1, 30),
                    Rent = 800,
                    LeaseType = Constants.RentPeriod.Monthly
                };
                IHttpActionResult leaseResult = leaseController.PostLease(lease);
                CreatedAtRouteNegotiatedContentResult<LeaseModel> leaseContentResult =
                    (CreatedAtRouteNegotiatedContentResult<LeaseModel>)leaseResult;

                createdLeaseId = leaseContentResult.Content.LeaseId;
            }

            //Act: Call DeleteProperty
            propertyResult = propertyController.DeleteProperty(propertyIdToDelete);

            //Assert:
            // Verify that HTTP result is OK
            // Verify that reading deleted property returns result not found
            Assert.IsInstanceOfType(propertyResult, typeof(OkNegotiatedContentResult<PropertyModel>));

            propertyResult = propertyController.GetProperty(propertyIdToDelete);
            Assert.IsInstanceOfType(propertyResult, typeof(NotFoundResult));

            // Verify that the lease created above was deleted
            using (var leaseController = new LeasesController())
            {
                IHttpActionResult leaseResult = leaseController.GetLease(createdLeaseId);
                Assert.IsInstanceOfType(leaseResult, typeof(NotFoundResult));
            }
        }
 public void Update(PropertyModel property)
 {
     Name = property.Name;
     Address1 = property.Address1;
     Address2 = property.Address2;
     City = property.City;
     State = property.State;
     Zip = property.Zip;         
 }
 public void Update(PropertyModel modelProperty)
 {
     // Copy values from input object to Property property
     Name = modelProperty.Name;
     Address1 = modelProperty.Address1;
     Address2 = modelProperty.Address2;
     City = modelProperty.City;
     State = modelProperty.State;
     Zipcode = modelProperty.Zipcode;
 }
        public IHttpActionResult PostProperty(PropertyModel property)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var dbProperty = new Property();

            dbProperty.Update(property);
            db.Properties.Add(dbProperty);
            db.SaveChanges();

            property.PropertyId = dbProperty.PropertyId;

            return CreatedAtRoute("DefaultApi", new { id = property.PropertyId }, property);
        }
        public IHttpActionResult PutProperty(int id, PropertyModel property)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != property.PropertyId)
            {
                return BadRequest();
            }

            var dbProperty = db.Properties.Find(id);

            dbProperty.Update(property);

            db.Entry(dbProperty).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PropertyExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        public void PostPropertyCreatesProperty()
        {
            //Arrange: Instantiate PropertiesController so its methods can be called
            var propertyController = new PropertiesController();

            //Act:
            // Create a PropertyModel object populated with test data,
            //  and call PostProperty
            var newProperty = new PropertyModel
            {
                Name="Huge Penthouse",
                Address1="Some address",
                City="New York",
                State="NY"
            };
            IHttpActionResult result = propertyController.PostProperty(newProperty);

            //Assert:
            // Verify that the HTTP result is CreatedAtRouteNegotiatedContentResult
            // Verify that the HTTP result body contains a nonzero property ID
            Assert.IsInstanceOfType
                (result, typeof(CreatedAtRouteNegotiatedContentResult<PropertyModel>));
            CreatedAtRouteNegotiatedContentResult<PropertyModel> contentResult =
                (CreatedAtRouteNegotiatedContentResult<PropertyModel>)result;
            Assert.IsTrue(contentResult.Content.PropertyId != 0);

            // Delete the test property
            result = propertyController.DeleteProperty(contentResult.Content.PropertyId);
        }
        public IHttpActionResult PostProperty(PropertyModel property)
        {
            // Validate the request
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            //Set up new Property object,
            //  and populate it with the values from
            //  the input PropertyModel object
            Property dbProperty = new Property();
            dbProperty.Update(property);

            // Add the new Property object to the list of Property objects
            db.Properties.Add(dbProperty);

            // Save the changes to the DB
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {

                throw new Exception("Unable to add the property to the database.");
            }

            // Update the PropertyModel object with the new property ID
            //  that was placed in the Property object after the changes
            //  were saved to the DB
            property.PropertyId = dbProperty.PropertyId;
            return CreatedAtRoute("DefaultApi", new { id = dbProperty.PropertyId }, property);
        }
        public IHttpActionResult PutProperty(int id, PropertyModel property)
        {
            // Validate the request
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != property.PropertyId)
            {
                return BadRequest();
            }

            if (!PropertyExists(id))
            {
                return BadRequest();
            }

            // Get the property record corresponding to the property ID, then
            //   update its properties to the values in the input PropertyModel object,
            //   and then set indicator that the record has been modified
            var dbProperty = db.Properties.Find(id);
            dbProperty.Update(property);
            db.Entry(dbProperty).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PropertyExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw new Exception("Unable to update the property in the database.");
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
        [TestMethod] //[3] | Create Property
        public void PostPropertyCreatesProperty()
        {
            //Arrange
            var propertiesController = new PropertiesController();

            //act 
            var newProp = new PropertyModel
            {
                Name = "Wonder Mansion",
                Address1 = "122 Wonka Way",
                Address2 = "",
                City = "Golden Coast",
                Zip = "23123",
                State = "CA"
            };

            //Result of the Post Request
            IHttpActionResult result = propertiesController.PostProperty(newProp);

            //Assert
            Assert.IsInstanceOfType(result, typeof(CreatedAtRouteNegotiatedContentResult<PropertyModel>));

            //Cast
            CreatedAtRouteNegotiatedContentResult<PropertyModel> contentResult = (CreatedAtRouteNegotiatedContentResult<PropertyModel>)result;

            Assert.IsTrue(contentResult.Content.PropertyId != 0);
        }
        [TestMethod] // [4] | Delete Property
        public void DeletePropertyDeleteProperty()
        {
            //Arrange
            var propertiesController = new PropertiesController();

            //act 
            var dbProp = new PropertyModel
            {
                Name = "Wonder Mansion",
                Address1 = "122 Wonka Way",
                Address2 = "",
                City = "Golden Coast",
                Zip = "23123",
                State = "CA"
            };

            //Add 'new property to database using post' 
            //Save returned value as RESULT
            IHttpActionResult result = propertiesController.PostProperty(dbProp);

            //Cast result as Content Result so I can gathere information from the content result
            CreatedAtRouteNegotiatedContentResult<PropertyModel> contentResult = (CreatedAtRouteNegotiatedContentResult<PropertyModel>)result;

            //Result contains the property I had just created
            result = propertiesController.GetProperty(contentResult.Content.PropertyId);

            //Get PropertyModel from result
            OkNegotiatedContentResult<PropertyModel> propertyResult = (OkNegotiatedContentResult<PropertyModel>)result;

            //Act
            result = propertiesController.DeleteProperty(contentResult.Content.PropertyId);

            //Assert

            //If action returns not found
            Assert.IsNotInstanceOfType(result, typeof(NotFoundResult));

            //If action retruns OK()
            Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<PropertyModel>));



        }