public async Task <WeaponResource> Create(WeaponResource entity) { var weapon = _mapper.Map <Weapon>(entity); // Ensure WeaponType is supplied if (weapon.WeaponType == null) { throw new InvalidModelException("weapon.WeaponType", "Null"); } var weaponType = await _weaponTypeRepository.GetByWeaponType(weapon.WeaponType.Color, weapon.WeaponType.Arm); weapon.WeaponTypeID = weaponType.ID; weapon.WeaponType = null; // Ensure WeaponCost is supplied if (weapon.WeaponCost == null) { throw new InvalidModelException("weapon.WeaponCost", "Null"); } // Insert and save await _weaponRepository.Insert(weapon); await _weaponRepository.SaveChanges(); return(_mapper.Map <WeaponResource>(weapon)); }
public async Task <IActionResult> Create([FromBody] WeaponResource resource) { if (resource == null) { throw new BadArguementException("Request body is null."); } if (!ModelState.IsValid) { throw new InvalidModelException(ModelState.ValidationState.ToString()); } WeaponResource result; result = await _service.Create(resource); return(CreatedAtRoute("GetWeapon", new { id = result.ID }, result)); }
public async Task Update(int ID, WeaponResource resource) { var entity = _mapper.Map <Weapon>(resource); // Ensure WeaponType is supplied if (entity.WeaponType == null) { throw new InvalidModelException("weapon.WeaponType", "Null"); } var weaponType = await _weaponTypeRepository.GetByWeaponType(entity.WeaponType.Color, entity.WeaponType.Arm); entity.WeaponTypeID = weaponType.ID; entity.WeaponType = null; // Ensure WeaponCost is supplied if (entity.WeaponCost == null) { throw new InvalidModelException("weapon.WeaponCost", "Null"); } // Update weapon await _weaponRepository.Update(entity); // Update weaponCost await _weaponCostRepository.Update(entity.WeaponCost); // Update WeaponEffectiveAgainst if (entity.WeaponEffectiveAgainst != null) { await _WeaponEffectiveAgainstRepository.Update(entity.WeaponEffectiveAgainst); } // Update WeaponStatChange if (entity.WeaponStatChange != null) { await _weaponStatChangeRepository.Update(entity.WeaponStatChange); } await _weaponRepository.SaveChanges(); }
public async Task <IActionResult> Update(int id, [FromBody] WeaponResource resource) { if (id < 1) { throw new BadArguementException($"Supplied ID is below starting value (Supplied: {id}, Required: id >= 1)."); } if (resource == null) { throw new ArgumentNullException("resource", "Supplied WeaponResource is null."); } if (!ModelState.IsValid) { throw new InvalidModelException(ModelState.ValidationState.ToString()); } if (id != resource.ID) { throw new ArgumentNullException("resource.ID", "Supplied WeaponResource.ID is null."); } await _service.Update(id, resource); return(NoContent()); }