Working game creation with publishers, developers, genres, and tags

dashboard
Pat Hartl 2023-01-10 17:17:54 -06:00
parent ddc68277b1
commit 3ae5971122
6 changed files with 116 additions and 30 deletions

View File

@ -242,7 +242,61 @@ namespace LANCommander.Controllers
{
if (ModelState.IsValid)
{
await GameService.Add(viewModel.Game);
var game = await GameService.Add(viewModel.Game);
if (viewModel.SelectedDevelopers != null && viewModel.SelectedDevelopers.Length > 0)
{
if (game.Developers == null)
game.Developers = new List<Company>();
foreach (var selectedDeveloper in viewModel.SelectedDevelopers)
{
var company = await CompanyService.AddMissing(c => c.Name == selectedDeveloper, new Company() { Name = selectedDeveloper });
game.Developers.Add(company);
}
}
if (viewModel.SelectedPublishers != null && viewModel.SelectedPublishers.Length > 0)
{
if (game.Publishers == null)
game.Publishers = new List<Company>();
foreach (var selectedPublisher in viewModel.SelectedPublishers)
{
var company = await CompanyService.AddMissing(c => c.Name == selectedPublisher, new Company() { Name = selectedPublisher });
game.Publishers.Add(company);
}
}
if (viewModel.SelectedGenres != null && viewModel.SelectedGenres.Length > 0)
{
if (game.Genres == null)
game.Genres = new List<Genre>();
foreach (var selectedGenre in viewModel.SelectedGenres)
{
var genre = await GenreService.AddMissing(g => g.Name == selectedGenre, new Genre() { Name = selectedGenre });
game.Genres.Add(genre);
}
}
if (viewModel.SelectedTags != null && viewModel.SelectedTags.Length > 0)
{
if (game.Tags == null)
game.Tags = new List<Tag>();
foreach (var selectedTag in viewModel.SelectedTags)
{
var tag = await TagService.AddMissing(g => g.Name == selectedTag, new Tag() { Name = selectedTag });
game.Tags.Add(tag);
}
}
await GameService.Update(game);
return RedirectToAction(nameof(Index));
}
@ -266,7 +320,7 @@ namespace LANCommander.Controllers
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind("Title,SortTitle,Description,ReleasedOn,Id,CreatedOn,CreatedById,UpdatedOn,UpdatedById")] Game game)
public async Task<IActionResult> Edit(Guid id, Game game)
{
if (id != game.Id)
{

View File

@ -16,16 +16,16 @@ namespace LANCommander.Data.Models
[Display(Name = "Released On")]
public DateTime? ReleasedOn { get; set; }
public virtual ICollection<Action> Actions { get; set; }
public virtual ICollection<Action>? Actions { get; set; }
public bool Singleplayer { get; set; } = false;
public virtual ICollection<MultiplayerMode> MultiplayerModes { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
public virtual ICollection<MultiplayerMode>? MultiplayerModes { get; set; }
public virtual ICollection<Genre>? Genres { get; set; }
public virtual ICollection<Tag>? Tags { get; set; }
public virtual ICollection<Category>? Categories { get; set; }
public virtual ICollection<Company> Publishers { get; set; }
public virtual ICollection<Company> Developers { get; set; }
public virtual ICollection<Company>? Publishers { get; set; }
public virtual ICollection<Company>? Developers { get; set; }
public virtual ICollection<Archive>? Archives { get; set; }
public string? ValidKeyRegex { get; set; }

View File

@ -29,7 +29,7 @@ namespace LANCommander.Migrations
b.HasIndex("GamesId");
b.ToTable("CategoryGame");
b.ToTable("CategoryGame", (string)null);
});
modelBuilder.Entity("GameDeveloper", b =>
@ -44,7 +44,7 @@ namespace LANCommander.Migrations
b.HasIndex("GameId");
b.ToTable("GameDeveloper");
b.ToTable("GameDeveloper", (string)null);
});
modelBuilder.Entity("GameGenre", b =>
@ -59,7 +59,7 @@ namespace LANCommander.Migrations
b.HasIndex("GenresId");
b.ToTable("GameGenre");
b.ToTable("GameGenre", (string)null);
});
modelBuilder.Entity("GamePublisher", b =>
@ -74,7 +74,7 @@ namespace LANCommander.Migrations
b.HasIndex("PublisherId");
b.ToTable("GamePublisher");
b.ToTable("GamePublisher", (string)null);
});
modelBuilder.Entity("GameTag", b =>
@ -89,7 +89,7 @@ namespace LANCommander.Migrations
b.HasIndex("TagsId");
b.ToTable("GameTag");
b.ToTable("GameTag", (string)null);
});
modelBuilder.Entity("LANCommander.Data.Models.Action", b =>
@ -140,7 +140,7 @@ namespace LANCommander.Migrations
b.HasIndex("UpdatedById");
b.ToTable("Actions");
b.ToTable("Actions", (string)null);
});
modelBuilder.Entity("LANCommander.Data.Models.Archive", b =>
@ -194,7 +194,7 @@ namespace LANCommander.Migrations
b.HasIndex("UpdatedById");
b.ToTable("Archive");
b.ToTable("Archive", (string)null);
});
modelBuilder.Entity("LANCommander.Data.Models.Category", b =>
@ -230,7 +230,7 @@ namespace LANCommander.Migrations
b.HasIndex("UpdatedById");
b.ToTable("Categories");
b.ToTable("Categories", (string)null);
});
modelBuilder.Entity("LANCommander.Data.Models.Company", b =>
@ -261,7 +261,7 @@ namespace LANCommander.Migrations
b.HasIndex("UpdatedById");
b.ToTable("Companies");
b.ToTable("Companies", (string)null);
});
modelBuilder.Entity("LANCommander.Data.Models.Game", b =>
@ -313,7 +313,7 @@ namespace LANCommander.Migrations
b.HasIndex("UpdatedById");
b.ToTable("Games");
b.ToTable("Games", (string)null);
});
modelBuilder.Entity("LANCommander.Data.Models.Genre", b =>
@ -344,7 +344,7 @@ namespace LANCommander.Migrations
b.HasIndex("UpdatedById");
b.ToTable("Genres");
b.ToTable("Genres", (string)null);
});
modelBuilder.Entity("LANCommander.Data.Models.Key", b =>
@ -404,7 +404,7 @@ namespace LANCommander.Migrations
b.HasIndex("UpdatedById");
b.ToTable("Keys");
b.ToTable("Keys", (string)null);
});
modelBuilder.Entity("LANCommander.Data.Models.MultiplayerMode", b =>
@ -455,7 +455,7 @@ namespace LANCommander.Migrations
b.HasIndex("UpdatedById");
b.ToTable("MultiplayerModes");
b.ToTable("MultiplayerModes", (string)null);
});
modelBuilder.Entity("LANCommander.Data.Models.Role", b =>
@ -513,7 +513,7 @@ namespace LANCommander.Migrations
b.HasIndex("UpdatedById");
b.ToTable("Tags");
b.ToTable("Tags", (string)null);
});
modelBuilder.Entity("LANCommander.Data.Models.User", b =>

View File

@ -7,10 +7,15 @@ namespace LANCommander.Models
{
public long? IgdbId { get; set; }
public Game Game { get; set; }
public List<SelectListItem> Genres { get; set; }
public List<SelectListItem> Tags { get; set; }
public List<SelectListItem> Categories { get; set; }
public List<SelectListItem> Developers { get; set; }
public List<SelectListItem> Publishers { get; set; }
public List<SelectListItem>? Genres { get; set; }
public List<SelectListItem>? Tags { get; set; }
public List<SelectListItem>? Categories { get; set; }
public List<SelectListItem>? Developers { get; set; }
public List<SelectListItem>? Publishers { get; set; }
public string[]? SelectedGenres { get; set; }
public string[]? SelectedTags { get; set; }
public string[]? SelectedCategories { get; set; }
public string[]? SelectedDevelopers { get; set; }
public string[]? SelectedPublishers { get; set; }
}
}

View File

@ -51,6 +51,33 @@ namespace LANCommander.Services
}
}
/// <summary>
/// Adds an entity to the database if it does exist as dictated by the predicate
/// </summary>
/// <param name="predicate">Qualifier expressoin</param>
/// <param name="entity">Entity to add</param>
/// <returns>Newly created or existing entity</returns>
public virtual async Task<T> AddMissing(Expression<Func<T, bool>> predicate, T entity)
{
using (var repo = new Repository<T>(Context, HttpContext))
{
var existing = repo.Get(predicate).FirstOrDefault();
if (existing == null)
{
entity = await repo.Add(entity);
await repo.SaveChanges();
return entity;
}
else
{
return existing;
}
}
}
public virtual async Task<T> Update(T entity)
{
using (var repo = new Repository<T>(Context, HttpContext))

View File

@ -53,24 +53,24 @@
<div class="mb-3">
<label asp-for="Developers" class="control-label"></label>
<input type="text" class="developer-select" />
<select asp-for="Developers" asp-items="Model.Developers" class="d-none"></select>
<select asp-for="SelectedDevelopers" class="d-none"></select>
</div>
<div class="mb-3">
<label asp-for="Publishers" class="control-label"></label>
<input type="text" class="publisher-select" />
<select asp-for="Publishers" asp-items="Model.Publishers" class="d-none"></select>
<select asp-for="SelectedPublishers" class="d-none"></select>
</div>
<div class="mb-3">
<label asp-for="Genres" class="control-label"></label>
<input type="text" class="genre-select" />
<select asp-for="Genres" asp-items="Model.Genres" class="d-none"></select>
<select asp-for="SelectedGenres" class="d-none"></select>
</div>
<div class="mb-3">
<label asp-for="Tags" class="control-label"></label>
<input type="text" class="tag-select" />
<select asp-for="Tags" asp-items="Model.Tags" class="d-none"></select>
<select asp-for="SelectedTags" class="d-none"></select>
</div>
</div>
</div>