Browse Source

limit to one playout per channel (#164)

pull/165/head
Jason Dove 5 years ago committed by GitHub
parent
commit
34fbfce0a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      ErsatzTV.Application/Playouts/Commands/CreatePlayoutHandler.cs
  2. 1
      ErsatzTV.Core/Interfaces/Repositories/IChannelRepository.cs
  3. 14
      ErsatzTV.Infrastructure/Data/Repositories/ChannelRepository.cs
  4. 4
      docs/user-guide/search.md

11
ErsatzTV.Application/Playouts/Commands/CreatePlayoutHandler.cs

@ -48,7 +48,7 @@ namespace ErsatzTV.Application.Playouts.Commands @@ -48,7 +48,7 @@ namespace ErsatzTV.Application.Playouts.Commands
}
private async Task<Validation<BaseError, Playout>> Validate(CreatePlayout request) =>
(await ChannelMustExist(request), await ProgramScheduleMustExist(request), ValidatePlayoutType(request))
(await ValidateChannel(request), await ProgramScheduleMustExist(request), ValidatePlayoutType(request))
.Apply(
(channel, programSchedule, playoutType) => new Playout
{
@ -57,10 +57,19 @@ namespace ErsatzTV.Application.Playouts.Commands @@ -57,10 +57,19 @@ namespace ErsatzTV.Application.Playouts.Commands
ProgramSchedulePlayoutType = playoutType
});
private Task<Validation<BaseError, Channel>> ValidateChannel(CreatePlayout createPlayout) =>
ChannelMustExist(createPlayout).BindT(ChannelMustNotHavePlayouts);
private async Task<Validation<BaseError, Channel>> ChannelMustExist(CreatePlayout createPlayout) =>
(await _channelRepository.Get(createPlayout.ChannelId))
.ToValidation<BaseError>("Channel does not exist.");
private async Task<Validation<BaseError, Channel>> ChannelMustNotHavePlayouts(Channel channel) =>
Optional(await _channelRepository.CountPlayouts(channel.Id))
.Filter(count => count == 0)
.Map(_ => channel)
.ToValidation<BaseError>("Channel already has one playout.");
private async Task<Validation<BaseError, ProgramSchedule>> ProgramScheduleMustExist(
CreatePlayout createPlayout) =>
(await _programScheduleRepository.GetWithPlayouts(createPlayout.ProgramScheduleId))

1
ErsatzTV.Core/Interfaces/Repositories/IChannelRepository.cs

@ -14,5 +14,6 @@ namespace ErsatzTV.Core.Interfaces.Repositories @@ -14,5 +14,6 @@ namespace ErsatzTV.Core.Interfaces.Repositories
Task<List<Channel>> GetAllForGuide();
Task Update(Channel channel);
Task Delete(int channelId);
Task<int> CountPlayouts(int channelId);
}
}

14
ErsatzTV.Infrastructure/Data/Repositories/ChannelRepository.cs

@ -1,6 +1,8 @@ @@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.Repositories;
using LanguageExt;
@ -12,8 +14,13 @@ namespace ErsatzTV.Infrastructure.Data.Repositories @@ -12,8 +14,13 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
public class ChannelRepository : IChannelRepository
{
private readonly TvContext _dbContext;
private readonly IDbConnection _dbConnection;
public ChannelRepository(TvContext dbContext) => _dbContext = dbContext;
public ChannelRepository(TvContext dbContext, IDbConnection dbConnection)
{
_dbContext = dbContext;
_dbConnection = dbConnection;
}
public async Task<Channel> Add(Channel channel)
{
@ -81,5 +88,10 @@ namespace ErsatzTV.Infrastructure.Data.Repositories @@ -81,5 +88,10 @@ namespace ErsatzTV.Infrastructure.Data.Repositories
_dbContext.Channels.Remove(channel);
await _dbContext.SaveChangesAsync();
}
public Task<int> CountPlayouts(int channelId) =>
_dbConnection.QuerySingleAsync<int>(
@"SELECT COUNT(*) FROM Playout WHERE ChannelId = @ChannelId",
new { ChannelId = channelId });
}
}

4
docs/user-guide/search.md

@ -24,11 +24,11 @@ Note that the `title` field is searched by default if no other field is specifie @@ -24,11 +24,11 @@ Note that the `title` field is searched by default if no other field is specifie
## Sample Searches
### Christmas Movies
### Christmas
`plot:christmas`
### Christmas Movies without Horror
### Christmas without Horror
`plot:christmas NOT genre:horror`

Loading…
Cancel
Save