-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
89 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 20 additions & 10 deletions
30
src/Weather.Infrastructure/Database/Repositories/WeatherCommandsRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,39 @@ | ||
using Ardalis.GuardClauses; | ||
using AutoMapper; | ||
using FluentResults; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
using Weather.Core.Abstractions; | ||
using Weather.Domain.Commands; | ||
using Weather.Domain.Logging; | ||
using Weather.Infrastructure.Database.EFContext; | ||
using Weather.Infrastructure.Database.EFContext.Entities; | ||
|
||
namespace Weather.Infrastructure.Database.Repositories | ||
{ | ||
internal sealed class WeatherCommandsRepository : IWeatherCommandsRepository | ||
internal sealed class WeatherCommandsRepository : RepositoryBase, IWeatherCommandsRepository | ||
{ | ||
private readonly IMapper _mapper; | ||
private readonly WeatherContext _weatherContext; | ||
public WeatherCommandsRepository(WeatherContext weatherContext, IMapper mapper) | ||
{ | ||
_weatherContext = Guard.Against.Null(weatherContext); | ||
_mapper = Guard.Against.Null(mapper); | ||
private readonly ILogger<IWeatherCommandsRepository> _logger; | ||
public WeatherCommandsRepository(WeatherContext weatherContext, IMapper mapper, ILogger<IWeatherCommandsRepository> logger) | ||
: base(weatherContext, mapper) | ||
{ | ||
_logger = Guard.Against.Null(logger); | ||
} | ||
|
||
public async Task<Result<int>> AddFavoriteLocation(AddFavoriteCommand addFavoriteCommand, CancellationToken cancellationToken) | ||
{ | ||
var locationEntity = _mapper.Map<FavoriteLocationEntity>(addFavoriteCommand.Location); | ||
await _weatherContext.FavoriteLocations.AddAsync(locationEntity); | ||
await _weatherContext.SaveChangesAsync(cancellationToken); | ||
return Result.Ok(locationEntity.Id); | ||
try | ||
{ | ||
await _weatherContext.FavoriteLocations.AddAsync(locationEntity); | ||
await _weatherContext.SaveChangesAsync(cancellationToken); | ||
return Result.Ok(locationEntity.Id); | ||
} | ||
catch(DbUpdateException ex) | ||
{ | ||
_logger.LogError(LogEvents.FavoriteWeathersStoreToDatabase, ex, "Can't add favorite locations into database."); | ||
return Result.Fail(ex.Message); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Ardalis.GuardClauses; | ||
using AutoMapper; | ||
using Weather.Infrastructure.Database.EFContext; | ||
|
||
namespace Weather.Infrastructure.Database | ||
{ | ||
internal abstract class RepositoryBase | ||
{ | ||
protected readonly IMapper _mapper; | ||
protected readonly WeatherContext _weatherContext; | ||
protected RepositoryBase(WeatherContext weatherContext, IMapper mapper) | ||
{ | ||
_weatherContext = Guard.Against.Null(weatherContext); | ||
_mapper = Guard.Against.Null(mapper); | ||
} | ||
} | ||
} |