Skip to content

Commit 7a8285a

Browse files
Converted WorldObjectControllers over to use IWorldObjectRepository
1 parent a9de688 commit 7a8285a

File tree

2 files changed

+53
-52
lines changed

2 files changed

+53
-52
lines changed
Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,154 @@
11
using LegendsViewer.Backend.Legends.WorldObjects;
22
using Microsoft.AspNetCore.Mvc;
3-
using LegendsViewer.Backend.Legends.Interfaces;
43
using LegendsViewer.Backend.Legends.EventCollections;
5-
using LegendsViewer.Backend.Legends.Events;
4+
using LegendsViewer.Backend.DataAccess.Repositories.Interfaces;
65

76
namespace LegendsViewer.Backend.Controllers;
87

9-
public class DanceFormController(IWorld worldDataService) : WorldObjectGenericController<DanceForm>(worldDataService.DanceForms, worldDataService.GetDanceForm)
8+
public class DanceFormController(IWorldObjectRepository<DanceForm> repository) : WorldObjectGenericController<DanceForm>(repository)
109
{
1110
}
1211

13-
public class MusicalFormController(IWorld worldDataService) : WorldObjectGenericController<MusicalForm>(worldDataService.MusicalForms, worldDataService.GetMusicalForm)
12+
public class MusicalFormController(IWorldObjectRepository<MusicalForm> repository) : WorldObjectGenericController<MusicalForm>(repository)
1413
{
1514
}
1615

17-
public class PoeticFormController(IWorld worldDataService) : WorldObjectGenericController<PoeticForm>(worldDataService.PoeticForms, worldDataService.GetPoeticForm)
16+
public class PoeticFormController(IWorldObjectRepository<PoeticForm> repository) : WorldObjectGenericController<PoeticForm>(repository)
1817
{
1918
}
2019

21-
public class WrittenContentController(IWorld worldDataService) : WorldObjectGenericController<WrittenContent>(worldDataService.WrittenContents, worldDataService.GetWrittenContent)
20+
public class WrittenContentController(IWorldObjectRepository<WrittenContent> repository) : WorldObjectGenericController<WrittenContent>(repository)
2221
{
2322
}
2423

25-
public class LandmassController(IWorld worldDataService) : WorldObjectGenericController<Landmass>(worldDataService.Landmasses, worldDataService.GetLandmass)
24+
public class LandmassController(IWorldObjectRepository<Landmass> repository) : WorldObjectGenericController<Landmass>(repository)
2625
{
2726
}
2827

29-
public class RiverController(IWorld worldDataService) : WorldObjectGenericController<River>(worldDataService.Rivers, worldDataService.GetRiver)
28+
public class RiverController(IWorldObjectRepository<River> repository) : WorldObjectGenericController<River>(repository)
3029
{
3130
}
3231

33-
public class SiteController(IWorld worldDataService) : WorldObjectGenericController<Site>(worldDataService.Sites, worldDataService.GetSite)
32+
public class SiteController(IWorldObjectRepository<Site> repository) : WorldObjectGenericController<Site>(repository)
3433
{
3534
}
3635

37-
public class RegionController(IWorld worldDataService) : WorldObjectGenericController<WorldRegion>(worldDataService.Regions, worldDataService.GetRegion)
36+
public class RegionController(IWorldObjectRepository<WorldRegion> repository) : WorldObjectGenericController<WorldRegion>(repository)
3837
{
3938
}
4039

41-
public class UndergroundRegionController(IWorld worldDataService) : WorldObjectGenericController<UndergroundRegion>(worldDataService.UndergroundRegions, worldDataService.GetUndergroundRegion)
40+
public class UndergroundRegionController(IWorldObjectRepository<UndergroundRegion> repository) : WorldObjectGenericController<UndergroundRegion>(repository)
4241
{
4342
}
4443

45-
public class ArtifactController(IWorld worldDataService) : WorldObjectGenericController<Artifact>(worldDataService.Artifacts, worldDataService.GetArtifact)
44+
public class ArtifactController(IWorldObjectRepository<Artifact> repository) : WorldObjectGenericController<Artifact>(repository)
4645
{
4746
}
4847

49-
public class EntityController(IWorld worldDataService) : WorldObjectGenericController<Entity>(worldDataService.Entities, worldDataService.GetEntity)
48+
public class EntityController(IWorldObjectRepository<Entity> repository) : WorldObjectGenericController<Entity>(repository)
5049
{
5150
[HttpGet("civs")]
5251
[ProducesResponseType(StatusCodes.Status200OK)]
5352
public ActionResult<List<Entity>> GetMainCivilizations()
5453
{
55-
return Ok(AllElements.Where(x => x.IsCiv || (x.EntityType == Legends.Enums.EntityType.Civilization && x.SiteHistory.Count > 0)));
54+
return Ok(Repository.GetAllElements().Where(x => x.IsCiv || (x.EntityType == Legends.Enums.EntityType.Civilization && x.SiteHistory.Count > 0)));
5655
}
5756
}
5857

59-
public class HistoricalFigureController(IWorld worldDataService) : WorldObjectGenericController<HistoricalFigure>(worldDataService.HistoricalFigures, worldDataService.GetHistoricalFigure)
58+
public class HistoricalFigureController(IWorldObjectRepository<HistoricalFigure> repository) : WorldObjectGenericController<HistoricalFigure>(repository)
6059
{
6160
}
6261

63-
public class MountainPeakController(IWorld worldDataService) : WorldObjectGenericController<MountainPeak>(worldDataService.MountainPeaks, worldDataService.GetMountainPeak)
62+
public class MountainPeakController(IWorldObjectRepository<MountainPeak> repository) : WorldObjectGenericController<MountainPeak>(repository)
6463
{
6564
}
6665

67-
public class StructureController(IWorld worldDataService) : WorldObjectGenericController<Structure>(worldDataService.Structures, worldDataService.GetStructure)
66+
public class StructureController(IWorldObjectRepository<Structure> repository) : WorldObjectGenericController<Structure>(repository)
6867
{
6968
}
7069

71-
public class ConstructionController(IWorld worldDataService) : WorldObjectGenericController<WorldConstruction>(worldDataService.WorldConstructions, worldDataService.GetWorldConstruction)
70+
public class ConstructionController(IWorldObjectRepository<WorldConstruction> repository) : WorldObjectGenericController<WorldConstruction>(repository)
7271
{
7372
}
7473

75-
public class EraController(IWorld worldDataService) : WorldObjectGenericController<Era>(worldDataService.Eras, worldDataService.GetEra)
74+
public class EraController(IWorldObjectRepository<Era> repository) : WorldObjectGenericController<Era>(repository)
7675
{
7776
}
7877

7978
// Warfare
8079

81-
public class WarController(IWorld worldDataService) : WorldObjectGenericController<War>(worldDataService.Wars, worldDataService.GetEventCollection<War>)
80+
public class WarController(IWorldObjectRepository<War> repository) : WorldObjectGenericController<War>(repository)
8281
{
8382
}
8483

85-
public class BattleController(IWorld worldDataService) : WorldObjectGenericController<Battle>(worldDataService.Battles, worldDataService.GetEventCollection<Battle>)
84+
public class BattleController(IWorldObjectRepository<Battle> repository) : WorldObjectGenericController<Battle>(repository)
8685
{
8786
}
8887

89-
public class DuelController(IWorld worldDataService) : WorldObjectGenericController<Duel>(worldDataService.Duels, worldDataService.GetEventCollection<Duel>)
88+
public class DuelController(IWorldObjectRepository<Duel> repository) : WorldObjectGenericController<Duel>(repository)
9089
{
9190
}
9291

93-
public class RaidController(IWorld worldDataService) : WorldObjectGenericController<Raid>(worldDataService.Raids, worldDataService.GetEventCollection<Raid>)
92+
public class RaidController(IWorldObjectRepository<Raid> repository) : WorldObjectGenericController<Raid>(repository)
9493
{
9594
}
9695

97-
public class SiteConqueredController(IWorld worldDataService) : WorldObjectGenericController<SiteConquered>(worldDataService.SiteConquerings, worldDataService.GetEventCollection<SiteConquered>)
96+
public class SiteConqueredController(IWorldObjectRepository<SiteConquered> repository) : WorldObjectGenericController<SiteConquered>(repository)
9897
{
9998
}
10099

101100
// Politcal Conflicts
102101

103-
public class InsurrectionController(IWorld worldDataService) : WorldObjectGenericController<Insurrection>(worldDataService.Insurrections, worldDataService.GetEventCollection<Insurrection>)
102+
public class InsurrectionController(IWorldObjectRepository<Insurrection> repository) : WorldObjectGenericController<Insurrection>(repository)
104103
{
105104
}
106105

107-
public class PersecutionController(IWorld worldDataService) : WorldObjectGenericController<Persecution>(worldDataService.Persecutions, worldDataService.GetEventCollection<Persecution>)
106+
public class PersecutionController(IWorldObjectRepository<Persecution> repository) : WorldObjectGenericController<Persecution>(repository)
108107
{
109108
}
110109

111-
public class PurgeController(IWorld worldDataService) : WorldObjectGenericController<Purge>(worldDataService.Purges, worldDataService.GetEventCollection<Purge>)
110+
public class PurgeController(IWorldObjectRepository<Purge> repository) : WorldObjectGenericController<Purge>(repository)
112111
{
113112
}
114113

115-
public class CoupController(IWorld worldDataService) : WorldObjectGenericController<EntityOverthrownCollection>(worldDataService.Coups, worldDataService.GetEventCollection<EntityOverthrownCollection>)
114+
public class CoupController(IWorldObjectRepository<EntityOverthrownCollection> repository) : WorldObjectGenericController<EntityOverthrownCollection>(repository)
116115
{
117116
}
118117

119-
public class BeastAttackController(IWorld worldDataService) : WorldObjectGenericController<BeastAttack>(worldDataService.BeastAttacks, worldDataService.GetEventCollection<BeastAttack>)
118+
public class BeastAttackController(IWorldObjectRepository<BeastAttack> repository) : WorldObjectGenericController<BeastAttack>(repository)
120119
{
121120
}
122121

123-
public class AbductionController(IWorld worldDataService) : WorldObjectGenericController<Abduction>(worldDataService.Abductions, worldDataService.GetEventCollection<Abduction>)
122+
public class AbductionController(IWorldObjectRepository<Abduction> repository) : WorldObjectGenericController<Abduction>(repository)
124123
{
125124
}
126125

127-
public class TheftController(IWorld worldDataService) : WorldObjectGenericController<Theft>(worldDataService.Thefts, worldDataService.GetEventCollection<Theft>)
126+
public class TheftController(IWorldObjectRepository<Theft> repository) : WorldObjectGenericController<Theft>(repository)
128127
{
129128
}
130129

131130
// Rituals
132131

133-
public class ProcessionController(IWorld worldDataService) : WorldObjectGenericController<ProcessionCollection>(worldDataService.Processions, worldDataService.GetEventCollection<ProcessionCollection>)
132+
public class ProcessionController(IWorldObjectRepository<ProcessionCollection> repository) : WorldObjectGenericController<ProcessionCollection>(repository)
134133
{
135134
}
136135

137-
public class PerformanceController(IWorld worldDataService) : WorldObjectGenericController<PerformanceCollection>(worldDataService.Performances, worldDataService.GetEventCollection<PerformanceCollection>)
136+
public class PerformanceController(IWorldObjectRepository<PerformanceCollection> repository) : WorldObjectGenericController<PerformanceCollection>(repository)
138137
{
139138
}
140139

141-
public class JourneyController(IWorld worldDataService) : WorldObjectGenericController<Journey>(worldDataService.Journeys, worldDataService.GetEventCollection<Journey>)
140+
public class JourneyController(IWorldObjectRepository<Journey> repository) : WorldObjectGenericController<Journey>(repository)
142141
{
143142
}
144143

145-
public class CompetitionController(IWorld worldDataService) : WorldObjectGenericController<CompetitionCollection>(worldDataService.Competitions, worldDataService.GetEventCollection<CompetitionCollection>)
144+
public class CompetitionController(IWorldObjectRepository<CompetitionCollection> repository) : WorldObjectGenericController<CompetitionCollection>(repository)
146145
{
147146
}
148147

149-
public class CeremonyController(IWorld worldDataService) : WorldObjectGenericController<CeremonyCollection>(worldDataService.Ceremonies, worldDataService.GetEventCollection<CeremonyCollection>)
148+
public class CeremonyController(IWorldObjectRepository<CeremonyCollection> repository) : WorldObjectGenericController<CeremonyCollection>(repository)
150149
{
151150
}
152151

153-
public class OccasionController(IWorld worldDataService) : WorldObjectGenericController<Occasion>(worldDataService.Occasions, worldDataService.GetEventCollection<Occasion>)
152+
public class OccasionController(IWorldObjectRepository<Occasion> repository) : WorldObjectGenericController<Occasion>(repository)
154153
{
155154
}

LegendsViewer.Backend/Controllers/WorldObjectGenericController.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using LegendsViewer.Backend.Contracts;
2+
using LegendsViewer.Backend.DataAccess.Repositories.Interfaces;
23
using LegendsViewer.Backend.Extensions;
34
using LegendsViewer.Backend.Legends;
45
using Microsoft.AspNetCore.Mvc;
@@ -7,11 +8,10 @@ namespace LegendsViewer.Backend.Controllers;
78

89
[ApiController]
910
[Route("api/[controller]")]
10-
public abstract class WorldObjectGenericController<T>(List<T> allElements, Func<int, T?> getById) : ControllerBase where T : WorldObject
11+
public abstract class WorldObjectGenericController<T>(IWorldObjectRepository<T> repository) : ControllerBase where T : WorldObject
1112
{
1213
private const int DefaultPageSize = 10;
13-
protected readonly List<T> AllElements = allElements;
14-
protected readonly Func<int, T?> GetById = getById;
14+
protected readonly IWorldObjectRepository<T> Repository = repository;
1515

1616
[HttpGet]
1717
[ProducesResponseType(StatusCodes.Status200OK)]
@@ -31,15 +31,15 @@ public ActionResult<PaginatedResponse<WorldObjectDto>> Get(
3131
}
3232

3333
// Filter world objects
34-
var filteredWorldObjects = AllElements
34+
var filteredWorldObjects = Repository.GetAllElements()
3535
.Where(worldObject =>
3636
string.IsNullOrWhiteSpace(search) ||
3737
worldObject.Name.Contains(search, StringComparison.InvariantCultureIgnoreCase) ||
3838
worldObject.Type?.Contains(search, StringComparison.InvariantCultureIgnoreCase) == true ||
3939
worldObject.Subtype?.Contains(search, StringComparison.InvariantCultureIgnoreCase) == true);
4040

4141
// Get total number of elements
42-
int totalElements = AllElements.Count;
42+
int totalElements = Repository.GetCount();
4343

4444
// Get total number of filtered elements
4545
int totalFilteredElements = filteredWorldObjects.Count();
@@ -71,24 +71,26 @@ public ActionResult<PaginatedResponse<WorldObjectDto>> Get(
7171
[ProducesResponseType(StatusCodes.Status404NotFound)]
7272
public ActionResult<T> Get([FromRoute] int id)
7373
{
74-
var item = GetById(id);
74+
var item = Repository.GetById(id);
7575
if (item == null)
7676
{
7777
return NotFound();
7878
}
79-
int currentIndex = AllElements.IndexOf(item);
80-
int previousIndex = currentIndex == 0 ? AllElements.Count - 1 : currentIndex - 1;
81-
int nextIndex = AllElements.Count == currentIndex + 1 ? 0 : currentIndex + 1;
82-
item.PreviousId = AllElements[previousIndex].Id;
83-
item.NextId = AllElements[nextIndex].Id;
79+
List<T> allElements = Repository.GetAllElements();
80+
81+
int currentIndex = allElements.IndexOf(item);
82+
int previousIndex = currentIndex == 0 ? allElements.Count - 1 : currentIndex - 1;
83+
int nextIndex = allElements.Count == currentIndex + 1 ? 0 : currentIndex + 1;
84+
item.PreviousId = allElements[previousIndex].Id;
85+
item.NextId = allElements[nextIndex].Id;
8486
return Ok(item);
8587
}
8688

8789
[HttpGet("count")]
8890
[ProducesResponseType(StatusCodes.Status200OK)]
8991
public ActionResult<int> GetCount()
9092
{
91-
return Ok(AllElements.Count);
93+
return Ok(Repository.GetCount());
9294
}
9395

9496
[HttpGet("{id}/events")]
@@ -102,7 +104,7 @@ public ActionResult<PaginatedResponse<WorldEventDto>> GetEvents(
102104
[FromQuery] string? sortKey = null,
103105
[FromQuery] string? sortOrder = null)
104106
{
105-
WorldObject? item = GetById(id);
107+
WorldObject? item = Repository.GetById(id);
106108
if (item == null)
107109
{
108110
return NotFound();
@@ -149,7 +151,7 @@ public ActionResult<PaginatedResponse<WorldObjectDto>> GetEventCollections(
149151
[FromQuery] string? sortKey = null,
150152
[FromQuery] string? sortOrder = null)
151153
{
152-
WorldObject? item = GetById(id);
154+
WorldObject? item = Repository.GetById(id);
153155
if (item == null)
154156
{
155157
return NotFound();
@@ -191,7 +193,7 @@ public ActionResult<PaginatedResponse<WorldObjectDto>> GetEventCollections(
191193
[ProducesResponseType(StatusCodes.Status404NotFound)]
192194
public ActionResult<ChartDataDto> GetEventChart([FromRoute] int id)
193195
{
194-
WorldObject? item = GetById(id);
196+
WorldObject? item = Repository.GetById(id);
195197
if (item == null)
196198
{
197199
return NotFound();

0 commit comments

Comments
 (0)