Having the clear locations endpoint only clear world locations, and adding endpoint to remove all items
This commit is contained in:
@@ -2,6 +2,7 @@ using GameAuthApi.Services;
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using GameAuthApi.Models;
|
||||||
|
|
||||||
namespace GameAuthApi.Controllers;
|
namespace GameAuthApi.Controllers;
|
||||||
|
|
||||||
@@ -23,5 +24,21 @@ public class AdminController : ControllerBase
|
|||||||
var status = await _adminService.GetStatusAsync();
|
var status = await _adminService.GetStatusAsync();
|
||||||
return Ok(status);
|
return Ok(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost("reset-locations")]
|
||||||
|
[Authorize(Roles = "SUPER")]
|
||||||
|
public async Task<IActionResult> ResetLocations()
|
||||||
|
{
|
||||||
|
var origin = await _adminService.ResetLocationsAsync();
|
||||||
|
return Ok(origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("clear-items")]
|
||||||
|
[Authorize(Roles = "SUPER")]
|
||||||
|
public async Task<IActionResult> ClearItems()
|
||||||
|
{
|
||||||
|
await _adminService.ClearItemsAsync();
|
||||||
|
return Ok("All items cleared.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using GameAuthApi.Services;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using GameAuthApi.Models;
|
||||||
|
|
||||||
public class AdminControllerTests
|
public class AdminControllerTests
|
||||||
{
|
{
|
||||||
@@ -10,7 +11,7 @@ public class AdminControllerTests
|
|||||||
public async Task GetStatus_ReturnsOk()
|
public async Task GetStatus_ReturnsOk()
|
||||||
{
|
{
|
||||||
var expected = new Dictionary<string, string> { { "status", "ok" } };
|
var expected = new Dictionary<string, string> { { "status", "ok" } };
|
||||||
var mockService = new Mock<AdminService>();
|
var mockService = new Mock<AdminService>((LocationService)null, (ItemService)null, (PlayerService)null);
|
||||||
mockService.Setup(s => s.GetStatusAsync()).ReturnsAsync(expected);
|
mockService.Setup(s => s.GetStatusAsync()).ReturnsAsync(expected);
|
||||||
var controller = new AdminController(mockService.Object);
|
var controller = new AdminController(mockService.Object);
|
||||||
|
|
||||||
@@ -19,5 +20,37 @@ public class AdminControllerTests
|
|||||||
var ok = Assert.IsType<OkObjectResult>(result);
|
var ok = Assert.IsType<OkObjectResult>(result);
|
||||||
Assert.Equal(expected, ok.Value);
|
Assert.Equal(expected, ok.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ResetLocations_ReturnsOk()
|
||||||
|
{
|
||||||
|
var newLoc = new Location { LocationType = "World", X = 0, Y = 0, Z = 0 };
|
||||||
|
var mockService = new Mock<AdminService>((LocationService)null, (ItemService)null, (PlayerService)null);
|
||||||
|
mockService.Setup(s => s.ResetLocationsAsync()).ReturnsAsync(newLoc);
|
||||||
|
var controller = new AdminController(mockService.Object);
|
||||||
|
|
||||||
|
var result = await controller.ResetLocations();
|
||||||
|
|
||||||
|
var ok = Assert.IsType<OkObjectResult>(result);
|
||||||
|
var origin = Assert.IsType<Location>(ok.Value);
|
||||||
|
Assert.Equal("World", origin.LocationType);
|
||||||
|
Assert.Equal(0, origin.X);
|
||||||
|
Assert.Equal(0, origin.Y);
|
||||||
|
Assert.Equal(0, origin.Z);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ClearItems_ReturnsOk()
|
||||||
|
{
|
||||||
|
var mockService = new Mock<AdminService>((LocationService)null, (ItemService)null, (PlayerService)null);
|
||||||
|
mockService.Setup(s => s.ClearItemsAsync()).Returns(Task.CompletedTask).Verifiable();
|
||||||
|
var controller = new AdminController(mockService.Object);
|
||||||
|
|
||||||
|
var result = await controller.ClearItems();
|
||||||
|
|
||||||
|
var ok = Assert.IsType<OkObjectResult>(result);
|
||||||
|
Assert.Equal("All items cleared.", ok.Value);
|
||||||
|
mockService.Verify();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
using GameAuthApi.Services;
|
||||||
|
using GameAuthApi.Models;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
public class AdminServiceTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task ResetLocations_DeletesWorldLocationsOnly()
|
||||||
|
{
|
||||||
|
var worldLocs = new List<Location> { new() { Id = "1", LocationType = "World" } };
|
||||||
|
|
||||||
|
var cfg = new ConfigurationBuilder()
|
||||||
|
.AddInMemoryCollection(new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{"MongoDB:ConnectionString", "mongodb://localhost"},
|
||||||
|
{"MongoDB:DatabaseName", "test"}
|
||||||
|
}).Build();
|
||||||
|
|
||||||
|
var locSvc = new Mock<LocationService>(cfg);
|
||||||
|
locSvc.Setup(l => l.GetByTypeAsync("World")).ReturnsAsync(worldLocs);
|
||||||
|
locSvc.Setup(l => l.DeleteByTypeAsync("World")).Returns(Task.CompletedTask).Verifiable();
|
||||||
|
locSvc.Setup(l => l.CreateAsync(It.IsAny<Location>())).Returns(Task.CompletedTask);
|
||||||
|
|
||||||
|
var itemSvc = new Mock<ItemService>(cfg);
|
||||||
|
itemSvc.Setup(i => i.DeleteByLocationAsync("1")).Returns(Task.CompletedTask).Verifiable();
|
||||||
|
|
||||||
|
var playerSvc = new Mock<PlayerService>(cfg);
|
||||||
|
playerSvc.Setup(p => p.GetAllAsync()).ReturnsAsync(new List<Player>());
|
||||||
|
|
||||||
|
var adminSvc = new AdminService(locSvc.Object, itemSvc.Object, playerSvc.Object);
|
||||||
|
|
||||||
|
await adminSvc.ResetLocationsAsync();
|
||||||
|
|
||||||
|
locSvc.Verify();
|
||||||
|
itemSvc.Verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ClearItems_RemovesAllItemsFromLocations()
|
||||||
|
{
|
||||||
|
var cfg = new ConfigurationBuilder()
|
||||||
|
.AddInMemoryCollection(new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{"MongoDB:ConnectionString", "mongodb://localhost"},
|
||||||
|
{"MongoDB:DatabaseName", "test"}
|
||||||
|
}).Build();
|
||||||
|
|
||||||
|
var locSvc = new Mock<LocationService>(cfg);
|
||||||
|
locSvc.Setup(l => l.ClearAllItemsAsync()).Returns(Task.CompletedTask).Verifiable();
|
||||||
|
|
||||||
|
var itemSvc = new Mock<ItemService>(cfg);
|
||||||
|
itemSvc.Setup(i => i.DeleteAllAsync()).Returns(Task.CompletedTask).Verifiable();
|
||||||
|
|
||||||
|
var playerSvc = new Mock<PlayerService>(cfg);
|
||||||
|
|
||||||
|
var adminSvc = new AdminService(locSvc.Object, itemSvc.Object, playerSvc.Object);
|
||||||
|
|
||||||
|
await adminSvc.ClearItemsAsync();
|
||||||
|
|
||||||
|
locSvc.Verify();
|
||||||
|
itemSvc.Verify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,10 +1,22 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using GameAuthApi.Models;
|
||||||
|
|
||||||
namespace GameAuthApi.Services;
|
namespace GameAuthApi.Services;
|
||||||
|
|
||||||
public class AdminService
|
public class AdminService
|
||||||
{
|
{
|
||||||
|
private readonly LocationService _locationService;
|
||||||
|
private readonly ItemService _itemService;
|
||||||
|
private readonly PlayerService _playerService;
|
||||||
|
|
||||||
|
public AdminService(LocationService locationService, ItemService itemService, PlayerService playerService)
|
||||||
|
{
|
||||||
|
_locationService = locationService;
|
||||||
|
_itemService = itemService;
|
||||||
|
_playerService = playerService;
|
||||||
|
}
|
||||||
|
|
||||||
public virtual Task<Dictionary<string, string>> GetStatusAsync()
|
public virtual Task<Dictionary<string, string>> GetStatusAsync()
|
||||||
{
|
{
|
||||||
var status = new Dictionary<string, string>
|
var status = new Dictionary<string, string>
|
||||||
@@ -13,5 +25,40 @@ public class AdminService
|
|||||||
};
|
};
|
||||||
return Task.FromResult(status);
|
return Task.FromResult(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual async Task<Location> ResetLocationsAsync()
|
||||||
|
{
|
||||||
|
var worldLocations = await _locationService.GetByTypeAsync("World");
|
||||||
|
foreach (var loc in worldLocations)
|
||||||
|
{
|
||||||
|
await _itemService.DeleteByLocationAsync(loc.Id);
|
||||||
|
}
|
||||||
|
await _locationService.DeleteByTypeAsync("World");
|
||||||
|
|
||||||
|
var origin = new Location
|
||||||
|
{
|
||||||
|
LocationType = "World",
|
||||||
|
X = 0,
|
||||||
|
Y = 0,
|
||||||
|
Z = 0
|
||||||
|
};
|
||||||
|
await _locationService.CreateAsync(origin);
|
||||||
|
|
||||||
|
var players = await _playerService.GetAllAsync();
|
||||||
|
foreach (var player in players)
|
||||||
|
{
|
||||||
|
player.LocationId = origin.Id;
|
||||||
|
await _playerService.UpdateAsync(player);
|
||||||
|
await _locationService.AddPlayerToLocationAsync(origin.Id, player.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual async Task ClearItemsAsync()
|
||||||
|
{
|
||||||
|
await _itemService.DeleteAllAsync();
|
||||||
|
await _locationService.ClearAllItemsAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,4 +44,14 @@ public class ItemService
|
|||||||
{
|
{
|
||||||
await _items.DeleteOneAsync(i => i.Id == id);
|
await _items.DeleteOneAsync(i => i.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual async Task DeleteByLocationAsync(string locationId)
|
||||||
|
{
|
||||||
|
await _items.DeleteManyAsync(i => i.LocationId == locationId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual async Task DeleteAllAsync()
|
||||||
|
{
|
||||||
|
await _items.DeleteManyAsync(_ => true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,17 @@ public class LocationService
|
|||||||
|
|
||||||
public virtual async Task DeleteAsync(string id) =>
|
public virtual async Task DeleteAsync(string id) =>
|
||||||
await _locations.DeleteOneAsync(loc => loc.Id == id);
|
await _locations.DeleteOneAsync(loc => loc.Id == id);
|
||||||
|
|
||||||
|
public virtual async Task<List<Location>> GetByTypeAsync(string type)
|
||||||
|
{
|
||||||
|
return await _locations.Find(l => l.LocationType == type).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual async Task DeleteByTypeAsync(string type) =>
|
||||||
|
await _locations.DeleteManyAsync(l => l.LocationType == type);
|
||||||
|
|
||||||
|
public virtual async Task DeleteAllAsync() =>
|
||||||
|
await _locations.DeleteManyAsync(_ => true);
|
||||||
public virtual async Task UpdateAsync(Location loc) =>
|
public virtual async Task UpdateAsync(Location loc) =>
|
||||||
await _locations.ReplaceOneAsync(l => l.Id == loc.Id, loc);
|
await _locations.ReplaceOneAsync(l => l.Id == loc.Id, loc);
|
||||||
|
|
||||||
@@ -83,4 +94,10 @@ public class LocationService
|
|||||||
var update = Builders<Location>.Update.Pull(l => l.Content, itemId);
|
var update = Builders<Location>.Update.Pull(l => l.Content, itemId);
|
||||||
await _locations.UpdateOneAsync(filter, update);
|
await _locations.UpdateOneAsync(filter, update);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual async Task ClearAllItemsAsync()
|
||||||
|
{
|
||||||
|
var update = Builders<Location>.Update.Set(l => l.Content, new List<string>());
|
||||||
|
await _locations.UpdateManyAsync(_ => true, update);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,11 @@ public class PlayerService
|
|||||||
public virtual async Task<Player> GetByIdAsync(string id) =>
|
public virtual async Task<Player> GetByIdAsync(string id) =>
|
||||||
await _players.Find(p => p.Id == id).FirstOrDefaultAsync();
|
await _players.Find(p => p.Id == id).FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
public virtual async Task<List<Player>> GetAllAsync()
|
||||||
|
{
|
||||||
|
return await _players.Find(_ => true).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
public virtual async Task CreateAsync(Player player) =>
|
public virtual async Task CreateAsync(Player player) =>
|
||||||
await _players.InsertOneAsync(player);
|
await _players.InsertOneAsync(player);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user