mirror of
https://github.com/XFox111/MuiCharts.git
synced 2026-04-22 06:51:05 +03:00
- Added /Import endpoints
- Minor refactoring and improved validation
This commit is contained in:
@@ -26,23 +26,13 @@ public class PointRepository(
|
||||
{
|
||||
_logger.LogInformation("Adding or updating point {point}", point);
|
||||
|
||||
bool doesExist = _context.Points.Any(p => p.Id == point.Id);
|
||||
Point result = UpsertPoint(point, out bool isNewlyCreated);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
if (doesExist)
|
||||
{
|
||||
_logger.LogInformation("Point {id} exists, updating", point.Id);
|
||||
_context.Points.Update(point);
|
||||
await _context.SaveChangesAsync();
|
||||
if (!isNewlyCreated)
|
||||
return (Point?)null;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Point {id} does not exist, adding", point.Id);
|
||||
EntityEntry<Point> result = _context.Points.Add(point);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return result.Entity;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -69,6 +59,29 @@ public class PointRepository(
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ErrorOr<IEnumerable<Point>>> AddPointsRangeAsync(IEnumerable<Point> points)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Adding points rage");
|
||||
|
||||
List<Point> updatedPoints = [];
|
||||
|
||||
foreach (Point point in points)
|
||||
updatedPoints.Add(UpsertPoint(point, out _));
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
_logger.LogInformation("Added {Count} points", updatedPoints.Count);
|
||||
|
||||
return updatedPoints;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error adding points {points}", points);
|
||||
return Error.Failure();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<ErrorOr<Deleted>> DeletePointAsync(int id)
|
||||
{
|
||||
@@ -81,6 +94,9 @@ public class PointRepository(
|
||||
if (point == null)
|
||||
return Error.NotFound();
|
||||
|
||||
if (_context.Tracks.Any(t => t.FirstId == id || t.SecondId == id))
|
||||
return Error.Conflict(description: "Point is used in a track. Delete track first");
|
||||
|
||||
_context.Points.Remove(point);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
@@ -119,4 +135,18 @@ public class PointRepository(
|
||||
{
|
||||
return Task.FromResult(_context.Points.AsQueryable());
|
||||
}
|
||||
|
||||
private Point UpsertPoint(Point point, out bool isNewlyCreated)
|
||||
{
|
||||
bool doesExist = _context.Points.Any(p => p.Id == point.Id);
|
||||
isNewlyCreated = !doesExist;
|
||||
Point entity;
|
||||
|
||||
if (doesExist)
|
||||
entity = _context.Points.Update(point).Entity;
|
||||
else
|
||||
entity = _context.Points.Add(point).Entity;
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,23 +32,13 @@ public class TrackRepository(
|
||||
return Error.Validation(description: "One or both specified points do not exist.");
|
||||
}
|
||||
|
||||
bool doesExist = _context.Tracks.Any(t => t.FirstId == track.FirstId && t.SecondId == track.SecondId);
|
||||
Track entity = UpsertTrack(track, out bool isNewlyCreated);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
if (doesExist)
|
||||
{
|
||||
_logger.LogInformation("Track with first ID {FirstId} and second ID {SecondId} exists, updating", track.FirstId, track.SecondId);
|
||||
_context.Tracks.Update(track);
|
||||
await _context.SaveChangesAsync();
|
||||
if (!isNewlyCreated)
|
||||
return (Track?)null;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Track with first ID {FirstId} and second ID {SecondId} does not exist, adding", track.FirstId, track.SecondId);
|
||||
EntityEntry<Track> result = _context.Tracks.Add(track);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return result.Entity;
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -88,6 +78,29 @@ public class TrackRepository(
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ErrorOr<IEnumerable<Track>>> AddTracksRangeAsync(IEnumerable<Track> tracks)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("Adding tracks range");
|
||||
|
||||
List<Track> updatedTracks = [];
|
||||
|
||||
foreach (Track track in tracks)
|
||||
updatedTracks.Add(UpsertTrack(track, out _));
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
_logger.LogInformation("Added {Count} tracks", updatedTracks.Count);
|
||||
|
||||
return updatedTracks;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error adding tracks {tracks}", tracks);
|
||||
return Error.Failure();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ErrorOr<Deleted>> DeleteTrackAsync(int firstId, int secondId)
|
||||
{
|
||||
@@ -145,6 +158,21 @@ public class TrackRepository(
|
||||
return Task.FromResult(_context.Tracks.AsQueryable());
|
||||
}
|
||||
|
||||
private Track UpsertTrack(Track track, out bool isNewlyCreated)
|
||||
{
|
||||
bool doesExist = _context.Tracks.Any(t => t.FirstId == track.FirstId && t.SecondId == track.SecondId);
|
||||
isNewlyCreated = !doesExist;
|
||||
|
||||
Track entity;
|
||||
|
||||
if (doesExist)
|
||||
entity = _context.Tracks.Update(track).Entity;
|
||||
else
|
||||
entity = _context.Tracks.Add(track).Entity;
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
private bool IsValidTrack(Track track)
|
||||
{
|
||||
return _context.Points.Any(p => p.Id == track.FirstId) &&
|
||||
|
||||
Reference in New Issue
Block a user