public IHttpActionResult PutCompany(int id, Company company)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult UpdateProperty(int id, Dto.Models.Property propertyDto)
        {
            loggedInUser = GetLoggedInUser();

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

            var result = new PropertyValidator().Validate(propertyDto);

            if (!result.IsValid)
            {
                return(new ValidatorError("Validation failed for updated property DTO", HttpStatusCode.BadRequest, result, Request));
            }

            if (id != propertyDto.Id)
            {
                return(new BadRequestErrorMessageResult("Updated property DTO id mismatch", this));
            }

            if (propertyDto.CompanyId != loggedInUser.CompanyId)
            {
                return(BadRequest("Updated property does not belong to same company as logged in user"));
            }
            else if (db.Properties.Count(p => p.CompanyId == loggedInUser.CompanyId && p.Id != propertyDto.Id && p.Name == propertyDto.Name) > 0)
            {
                return(new BadRequestErrorMessageResult("Another property has the same name as this property", this));
            }

            var propertyEntity = Mapper.Map <Dto.Models.Property, Entity.Models.Property>(propertyDto);

            db.Properties.Attach(propertyEntity);

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

            if (propertyDto.Users != null)
            {
                // Update Users for Property
                db.Entry(propertyEntity).Collection(u => u.Users).Load(); // force load
                var userIdList = propertyDto.Users.Select(u => u.Id);
                var newUsers   = db.Users.Where(u => userIdList.Contains(u.Id)).ToList();
                propertyEntity.Users = newUsers; // for this to work, existing Users must have been forced loaded.
            }

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
示例#3
0
        public IHttpActionResult UpdateUser(int id, Dto.Models.User userDto)
        {
            loggedInUser = GetLoggedInUser();

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

            if (userDto == null)
            {
                return(new BadRequestErrorMessageResult("Updated user DTO is missing", this));
            }

            ValidationResult userValidatorResult = new UserValidator().Validate(userDto);

            if (!userValidatorResult.IsValid)
            {
                return(new ValidatorError("Validation failed for updated user DTO", HttpStatusCode.BadRequest, userValidatorResult, Request));
            }

            if (id != userDto.Id)
            {
                return(new BadRequestErrorMessageResult("Updated user DTO id mismatch", this));
            }

            var userEntity = Mapper.Map <Dto.Models.User, Entity.Models.User>(userDto);

            if (userEntity.CompanyId != loggedInUser.CompanyId)
            {
                // Updated user does not have same company. Make it appear as user does not exist for this company.
                return(NotFound());
            }
            else if (db.Users.Count(u => u.CompanyId == loggedInUser.CompanyId && u.Id != userEntity.Id && u.Username == userEntity.Username) > 0)
            {
                return(new BadRequestErrorMessageResult("Another user has the same username as this user", this));
            }

            db.Users.Attach(userEntity);
            // Don't mark entire entity as modified - fields are optional
            //db.Entry(userEntity).State = EntityState.Modified;

            if (userEntity.Fullname != null)
            {
                db.Entry(userEntity).Property(u => u.Fullname).IsModified = true;
            }

            if (userEntity.Username != null)
            {
                db.Entry(userEntity).Property(u => u.Username).IsModified = true;
            }

            if (userEntity.Password != null)
            {
                db.Entry(userEntity).Property(u => u.Password).IsModified = true;
            }

            if (userEntity.Password == null)
            {
                // Entity validation will fail because Password column is not-null and password is optional field.
                // NOTE: Must use Where/Select instead of Find, so entire entity is not loaded (otherwise it will conflict with Attach!)
                //userEntity.Password = db.Users.Where(u => u.Id == userEntity.Id).Select(u => u.Password).FirstOrDefault();
            }

            if (userDto.Properties != null)
            {
                db.Entry(userEntity).Collection(u => u.Properties).Load(); // force load
                var propertyIdList = userDto.Properties.Select(p => p.Id);
                var newProperties  = db.Properties.Where(p => propertyIdList.Contains(p.Id)).ToList();

                userEntity.Properties = newProperties; // for this to work you must force load existing Property collection
            }

            try
            {
                db.Configuration.ValidateOnSaveEnabled = false;
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                db.Configuration.ValidateOnSaveEnabled = true;
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }