public IHttpActionResult PutBuildingToTenant(int id, BuildingToTenant buildingToTenant)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != buildingToTenant.BuildingId)
            {
                return(BadRequest());
            }

            _db.Entry(buildingToTenant).State = EntityState.Modified;

            try {
                _db.SaveChanges();
            } catch (DbUpdateConcurrencyException) {
                if (!BuildingToTenantExists(id))
                {
                    return(NotFound());
                }
                throw;
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PostBuildingToTenant(BuildingToTenant buildingToTenant)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _db.BuildingToTenants.Add(buildingToTenant);

            try {
                _db.SaveChanges();
            } catch (DbUpdateException) {
                if (BuildingToTenantExists(buildingToTenant.BuildingId))
                {
                    return(Conflict());
                }
                throw;
            }
            Tenant tenant = _db.Tenants.Find(buildingToTenant.TenantId);

            if (tenant == null)
            {
                return(Conflict());
            }

            return(CreatedAtRoute("DefaultApi", new { id = buildingToTenant.BuildingId }, new { tenant.Name, tenant.Id, buildingToTenant.ExpirationDate }));
        }
        public IHttpActionResult DeleteBuildingToTenant(int id, int tenantId)
        {
            BuildingToTenant buildingToTenant = _db.BuildingToTenants.First(b => b.BuildingId == id && b.TenantId == tenantId);

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

            _db.BuildingToTenants.Remove(buildingToTenant);
            _db.SaveChanges();

            return(Ok(buildingToTenant));
        }
        protected override void Seed(PropertyCatalogContext context)
        {
            Enumerable.Range(0, 20).Select(x => new Building {
                Name               = Truncate(Faker.CompanyFaker.Name(), 30),
                NumberOfFloors     = Faker.NumberFaker.Number(1, 199),
                City               = Truncate(Faker.LocationFaker.City(), 30),
                ZipCode            = $"0{Faker.LocationFaker.ZipCode()}",
                Address            = Truncate(Faker.LocationFaker.Street(), 30),
                YearOfConstruction = Faker.DateTimeFaker.DateTimeBetweenYears(50)
            }).ToList().ForEach(b => context.Buildings.Add(b));

            context.SaveChanges();

            Enumerable.Range(0, 100).Select(x => new Tenant {
                Name = Truncate(Faker.NameFaker.Name(), 20)
            }).ToList().ForEach(t => context.Tenants.Add(t));

            context.SaveChanges();

            foreach (Building building in context.Buildings)
            {
                var buildingToTenant1 = new BuildingToTenant {
                    BuildingId     = building.Id,
                    TenantId       = Faker.NumberFaker.Number(1, 49),
                    ExpirationDate = Faker.DateTimeFaker.DateTimeBetweenYears(10, 20)
                };
                var buildingToTenant2 = new BuildingToTenant {
                    BuildingId     = building.Id,
                    TenantId       = Faker.NumberFaker.Number(50, 99),
                    ExpirationDate = Faker.DateTimeFaker.DateTimeBetweenYears(10, 20)
                };
                context.BuildingToTenants.Add(buildingToTenant1);
                context.BuildingToTenants.Add(buildingToTenant2);
            }

            context.SaveChanges();
        }