예제 #1
0
        public LeaseBase LeaseAdd(LeaseAdd newItem)
        {
            if (newItem == null)
            {
                return(null);
            }
            else
            {
                var associatedApartment = ds.Apartments.Find(newItem.ApartmentNumber);
                if (associatedApartment == null)
                {
                    return(null);
                }

                var associatedTenant = ds.Tenants.Find(newItem.TenantId);
                if (associatedTenant == null)
                {
                    return(null);
                }

                Lease addedItem = Mapper.Map <Lease>(newItem);

                addedItem.Apartment = associatedApartment;
                addedItem.Tenant    = associatedTenant;
                ds.Leases.Add(addedItem);

                var editedApt = Mapper.Map <ApartmentBase>(associatedApartment);
                editedApt.Status = "Occupied";
                ds.Entry(associatedApartment).CurrentValues.SetValues(editedApt);

                ds.SaveChanges();

                return(Mapper.Map <LeaseBase>(addedItem));
            }
        }
        public async Task <IHttpActionResult> Post([FromBody] LeaseAdd newItem)
        {
            if (Request.GetRouteData().Values["id"] != null)
            {
                return(BadRequest("Invalid request URI"));
            }

            if (newItem == null)
            {
                return(BadRequest("Must send an entity body with the request"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var apartment = m.ApartmentGetById(newItem.ApartmentNumber);

            if (apartment == null)
            {
                return(Content(HttpStatusCode.NotFound, "Apartment Number not found"));
            }

            var lease = m.LeaseGetByAptNumber(newItem.ApartmentNumber);

            if (lease != null)
            {
                return(Content(HttpStatusCode.Conflict, "Apartment already associated with a lease"));
            }

            var addedItem = m.LeaseAdd(newItem);

            if (addedItem == null)
            {
                return(BadRequest("Cannot add the object"));
            }


            var uri = Url.Link("DefaultApi", new { id = addedItem.Id });

            // SEND ACTIVATION CODE TO TENANT
            var hashPassword = m.TenantAddCode(addedItem.TenantId);

            var tenant = m.TenantGetById(addedItem.TenantId);

            await sendEmail(hashPassword, tenant.Email);

            return(Created(uri, addedItem));
        }