public IHttpActionResult GetPropertyTaskListType(int id)
        {
            PropertyTaskListType propertyTaskListType = db.PropertyTaskListTypes.Find(id);

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

            return(Ok(propertyTaskListType));
        }
        public IHttpActionResult PostPropertyTaskListType(PropertyTaskListType propertyTaskListType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.PropertyTaskListTypes.Add(propertyTaskListType);
            db.SaveChanges();

            return(Ok(propertyTaskListType));
        }
        public IHttpActionResult PutPropertyTaskListType(int id, PropertyTaskListType propertyTaskListType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != propertyTaskListType.Id)
            {
                return(BadRequest());
            }

            var originalPropertyTaskListType = db.PropertyTaskListTypes
                                               .Where(t => t.Id == propertyTaskListType.Id)
                                               .Include(t => t.PropertyTaskHeaders)
                                               .SingleOrDefault();

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

            var originalEntry = db.Entry(originalPropertyTaskListType);

            originalEntry.CurrentValues.SetValues(propertyTaskListType);

            foreach (var header in propertyTaskListType.PropertyTaskHeaders)
            {
                var originalHeader = originalPropertyTaskListType.PropertyTaskHeaders
                                     .SingleOrDefault(h => h.Id == header.Id && h.Id != 0);

                if (originalHeader != null)
                {
                    if (string.IsNullOrWhiteSpace(header.Name))
                    {
                        db.PropertyTaskDetails.Where(d => d.PropertyTaskHeaderId == originalHeader.Id)
                        .ToList()
                        .ForEach(d => db.PropertyTaskDetails.Remove(d));
                        db.PropertyTaskHeaders.Remove(originalHeader);
                    }
                    else
                    {
                        var headerEntry = db.Entry(originalHeader);
                        headerEntry.CurrentValues.SetValues(header);
                    }
                }
                else
                {
                    header.Id = 0;
                    originalPropertyTaskListType.PropertyTaskHeaders.Add(header);
                }
            }

            foreach (var originalHeader in originalPropertyTaskListType.PropertyTaskHeaders.Where(h => h.Id != 0).Where(originalHeader => propertyTaskListType.PropertyTaskHeaders.All(h => h.Id != originalHeader.Id)).ToList())
            {
                db.PropertyTaskDetails.Where(d => d.PropertyTaskHeaderId == originalHeader.Id)
                .ToList()
                .ForEach(d => db.PropertyTaskDetails.Remove(d));
                db.PropertyTaskHeaders.Remove(originalHeader);
            }

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

            return(StatusCode(HttpStatusCode.NoContent));
        }