Beispiel #1
0
        public async Task <IActionResult> PutCompany([FromRoute] int id, [FromBody] Company company)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CompanyExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        private ResponseBase InitializeUpdate(UpdateCommand command, KeyValuePair <Type, string> property,
                                              HttpInformation context, SapphireDbContext db)
        {
            object updateValue = command.UpdateValue.ToObject(property.Key);

            if (!property.Key.CanUpdate(context, updateValue, serviceProvider))
            {
                return(command.CreateExceptionResponse <UpdateResponse>("The user is not authorized for this action."));
            }

            object[] primaryKeys = property.Key.GetPrimaryKeyValues(db, updateValue);
            object   value       = db.Find(property.Key, primaryKeys);

            if (value != null)
            {
                db.Entry(value).State = EntityState.Detached;
                return(SaveChangesToDb(property, value, updateValue, db, context, command));
            }

            return(command.CreateExceptionResponse <UpdateResponse>("No value to update was found"));
        }
        private ResponseBase InitializeUpdate(UpdateRangeCommand command, KeyValuePair <Type, string> property,
                                              HttpInformation context, SapphireDbContext db)
        {
            object[] updateValues = command.UpdateValues.Values <JObject>().Select(newValue => newValue.ToObject(property.Key)).ToArray();

            UpdateRangeResponse response = new UpdateRangeResponse
            {
                ReferenceId = command.ReferenceId,
                Results     = updateValues.Select(updateValue =>
                {
                    if (!property.Key.CanUpdate(context, updateValue, serviceProvider))
                    {
                        return(command.CreateExceptionResponse <UpdateResponse>(
                                   "The user is not authorized for this action."));
                    }

                    object[] primaryKeys = property.Key.GetPrimaryKeyValues(db, updateValue);
                    object value         = db.Find(property.Key, primaryKeys);

                    if (value != null)
                    {
                        db.Entry(value).State = EntityState.Detached;
                        return(ApplyChangesToDb(property, value, updateValue, db, context));
                    }

                    return(command.CreateExceptionResponse <UpdateResponse>("No value to update was found"));
                }).ToList()
            };


            db.SaveChanges();

            foreach (object value in updateValues)
            {
                property.Key.ExecuteHookMethods <UpdateEventAttribute>(ModelStoreEventAttributeBase.EventType.After, value, context, serviceProvider);
            }

            return(response);
        }