public async Task <IActionResult> CreateFromList([FromBody] IEnumerable <WeaponResource> resource) { if (resource == null) { throw new BadArguementException("Request body is null."); } if (!ModelState.IsValid) { throw new InvalidModelException(ModelState.ValidationState.ToString()); } await _service.CreateFromList(resource); return(CreatedAtRoute("GetAllWeapons", new { })); }
public static async Task Initialize(FehContext context, IWeaponService weaponService, IAuthService authService, IConfiguration configuration) { context.Database.EnsureCreated(); if (!context.WeaponTypes.Any()) { try { var weaponTypes = JsonConvert.DeserializeObject <List <WeaponType> >(File.ReadAllText("Seed" + Path.DirectorySeparatorChar + GetFileName("WeaponType"))); foreach (var wt in weaponTypes) { context.WeaponTypes.Add(wt); } context.SaveChanges(); } catch (Exception e) { throw e; } } if (!context.MovementTypes.Any()) { try { var movementTypes = JsonConvert.DeserializeObject <List <MovementType> >(File.ReadAllText("Seed" + Path.DirectorySeparatorChar + GetFileName("MovementType"))); foreach (var mt in movementTypes) { context.MovementTypes.Add(mt); } context.SaveChanges(); } catch (Exception e) { throw e; } } if (!context.Weapons.Any()) { try { var weapons = JsonConvert.DeserializeObject <List <WeaponResource> >(File.ReadAllText("Seed" + Path.DirectorySeparatorChar + GetFileName("Weapons"))); var refinedWeapons = JsonConvert.DeserializeObject <List <WeaponResource> >(File.ReadAllText("Seed" + Path.DirectorySeparatorChar + GetFileName("WeaponsRefined"))); // gets images for refined weapons (since they have the same image as the unrefined versions) for (int i = 0; i < refinedWeapons.Count(); i++) { if (string.IsNullOrEmpty(refinedWeapons[i].ImageUri)) { var split = refinedWeapons[i].Name.Split('(')[0]; var fixedLength = split.Substring(0, split.Length - 1); var weaponWhere = weapons.Where(w => w.Name.Contains(fixedLength)); refinedWeapons[i].ImageUri = weaponWhere.First().ImageUri; } } await weaponService.CreateFromList(weapons); await weaponService.CreateFromList(refinedWeapons); } catch (Exception e) { throw e; } } if (!context.Users.Any()) { try { UserResource userEntry = new UserResource() { Username = "******", Password = "******" }; await authService.CreateAccount(userEntry); } catch (Exception e) { throw e; } } }