public void DeleteLeaseDeletesLease()
        {
            //Arrange:
            // Instantiate LeasesController so its methods can be called
            // Create a new lease to be deleted, and get its lease ID
            var leaseController = new LeasesController();

            var lease = new LeaseModel
            {
                CreatedDate = new DateTime(2014, 9, 30),
                PropertyId = 1,
                TenantId = 1,
                StartDate = new DateTime(2015, 1, 30),
                Rent = 800,
                LeaseType = Constants.RentPeriod.Monthly
            };
            IHttpActionResult result = leaseController.PostLease(lease);
            CreatedAtRouteNegotiatedContentResult<LeaseModel> contentResult =
                (CreatedAtRouteNegotiatedContentResult<LeaseModel>)result;

            int leaseIdToDelete = contentResult.Content.LeaseId;

            //Act: Call DeleteLease
            result = leaseController.DeleteLease(leaseIdToDelete);

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

            result = leaseController.GetLease(leaseIdToDelete);
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
        public IHttpActionResult PostLease(LeaseModel lease)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            //Set up new Lease object,
            //  and populate it with the values from
            //  the input LeaseModel object
            Lease dbLease = new Lease();
            dbLease.Update(lease);

            // Add the new Lease object to the list of Lease objects
            db.Leases.Add(dbLease);

            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {

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

            // Update the LeaseModel object with the new lease ID
            //  that was placed in the Lease object after the changes
            //  were saved to the DB
            lease.LeaseId = dbLease.LeaseId;
            return CreatedAtRoute("DefaultApi", new { id = dbLease.LeaseId }, lease);
        }
        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 DeleteTenantDeletesTenant()
        {
            //Arrange:
            // Instantiate TenantsController so its methods can be called
            // Create a new tenant to be deleted, and get its tenant ID
            var tenantController = new TenantsController();

            var tenant = new TenantModel
            {
                FirstName = "Testy",
                LastName = "Testering",
                Telephone = "555-1212",
                EmailAddress = "*****@*****.**"
            };
            IHttpActionResult result = tenantController.PostTenant(tenant);
            CreatedAtRouteNegotiatedContentResult<TenantModel> contentResult =
                (CreatedAtRouteNegotiatedContentResult<TenantModel>)result;

            int tenantIdToDelete = contentResult.Content.TenantId;

            // Add a lease corresponding to the tenant
            int createdLeaseId;
            using (var leaseController = new LeasesController())
            {
                var lease = new LeaseModel
                {
                    CreatedDate = new DateTime(2014, 9, 30),
                    PropertyId = 1,
                    TenantId = tenantIdToDelete,
                    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 DeleteTenant
            result = tenantController.DeleteTenant(tenantIdToDelete);

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

            result = tenantController.GetTenant(tenantIdToDelete);
            Assert.IsInstanceOfType(result, 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 IHttpActionResult PostLease(LeaseModel lease)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var dbLease = new Lease();

            db.Leases.Add(dbLease);
            db.SaveChanges();

            lease.LeaseId = dbLease.LeaseId;

            return CreatedAtRoute("DefaultApi", new { id = lease.LeaseId }, lease);
        }
        public IHttpActionResult PutLease(int id, LeaseModel lease)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != lease.LeaseId)
            {
                return BadRequest();
            }

            var dbLease = db.Leases.Find(id);

            dbLease.Update(lease);

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

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

            return StatusCode(HttpStatusCode.NoContent);
        }
Esempio n. 7
0
 public void Update(LeaseModel lease)
 {
     StartDate = lease.StartDate;
     EndDate = lease.EndDate;
     Rent = lease.Rent;
 }
        public void PostLeaseCreatesLease()
        {
            //Arrange: Instantiate LeasesController so its methods can be called
            var leaseController = new LeasesController();

            //Act:
            // Create a LeaseModel object populated with test data,
            //  and call PostLease
            var newLease = new LeaseModel
            {
                CreatedDate = new DateTime(2014, 9, 30),
                PropertyId = 1,
                TenantId = 1,
                StartDate = new DateTime(2015, 1, 30),
                Rent = 800,
                LeaseType = Constants.RentPeriod.Monthly
            };
            IHttpActionResult result = leaseController.PostLease(newLease);

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

            // Delete the test lease
            result = leaseController.DeleteLease(contentResult.Content.LeaseId);
        }
Esempio n. 9
0
        public void Update(LeaseModel modelLease)
        {
            // If adding new lease, set created date
            if (modelLease.LeaseId == 0)
            {
                CreatedDate = DateTime.Now;
            }

            // Copy values from input object to Lease lease
            PropertyId = modelLease.PropertyId;
            TenantId = modelLease.TenantId;
            StartDate = modelLease.StartDate;
            EndDate = modelLease.EndDate;
            Rent = modelLease.Rent;
            LeaseType = modelLease.LeaseType;
        }
        public IHttpActionResult PutLease(int id, LeaseModel lease)
        {
            // Validate the request
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != lease.LeaseId)
            {
                return BadRequest();
            }

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

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

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

            return StatusCode(HttpStatusCode.NoContent);
        }