public async Task <int> Handle(CreateFilmCommand request, CancellationToken cancellationToken)
        {
            using (var liteDatabase = _provider.GetDatabase())
            {
                var film   = liteDatabase.GetCollection <Film>();
                var entity = new Film()
                {
                    Title = request.Title,
                    Year  = request.Year
                };

                if (request.MediaType != null)
                {
                    entity.Medias.Add(new Media()
                    {
                        Type = request.MediaType.Value, Amount = request.MediaAmount ?? 1
                    });
                }

                if (request.ExternalSource != null && !string.IsNullOrEmpty(request.ExternalId))
                {
                    entity.ExternalId     = request.ExternalId;
                    entity.ExternalSource = request.ExternalSource;
                }

                return(film.Insert(entity).AsInt32);
            }
        }
示例#2
0
 public async Task <IEnumerable <Storage> > Handle(ListStorageQuery request, CancellationToken cancellationToken)
 {
     using (var db = _provider.GetDatabase())
     {
         var storageCollection = db.GetCollection <Storage>();
         return(storageCollection.Find(Query.All("Title"), request.Page, request.PageSize));
     }
 }
 public async Task <IEnumerable <Film> > Handle(ListFilmWithNoMedia request, CancellationToken cancellationToken)
 {
     using (var liteDatabase = _provider.GetDatabase())
     {
         var liteCollection = liteDatabase.GetCollection <Film>();
         return(liteCollection.Find(x => x.Medias.Count == 0, request.Page, request.PageSize));
     }
 }
示例#4
0
        public async Task <IEnumerable <Film> > Handle(ListFilmsQuery request, CancellationToken cancellationToken)
        {
            using (var liteDatabase = _provider.GetDatabase())
            {
                var collection = liteDatabase.GetCollection <Film>()
                                 .Include(x => x.Storages);

                return(collection.Find(Query.All("Title"), request.Page * request.PageSize, request.PageSize));
            }
        }
示例#5
0
 public async Task <int> Handle(CreateStorageCommand request, CancellationToken cancellationToken)
 {
     using (var liteDatabase = _provider.GetDatabase())
     {
         var storages = liteDatabase.GetCollection <Storage>();
         return(storages.Insert(new Storage()
         {
             Title = request.Title
         }).AsInt32);
     }
 }
 public async Task Handle(AddMediaToFilm message, CancellationToken cancellationToken)
 {
     using (var liteDatabase = _provider.GetDatabase())
     {
         var filmCollection = liteDatabase.GetCollection <Film>();
         var film           = filmCollection.FindById(message.FilmId);
         film.Medias.Add(new Media()
         {
             Amount = message.Amount, Type = message.TypeOfMedia
         });
         filmCollection.Update(film);
     }
 }
示例#7
0
        public async Task <IEnumerable <Film> > Handle(ListFilmsWithQuery request, CancellationToken cancellationToken)
        {
            using (var liteDatabase = _provider.GetDatabase())
            {
                var filmCollection = liteDatabase
                                     .GetCollection <Film>()
                                     .Include(x => x.Storages);

                if (!string.IsNullOrWhiteSpace(request.Query))
                {
                    return(filmCollection.Find(Query.Contains("Title", request.Query), request.Page * request.PageSize, request.PageSize));
                }

                return(filmCollection.Find(Query.All(), request.Page * request.PageSize, request.PageSize));
            }
        }
        public Task Process(CreateFilmCommand request, CancellationToken cancellationToken)
        {
            if (request.ExternalSource != null && request.ExternalId != null)
            {
                using (var liteDatabase = _provider.GetDatabase())
                {
                    var collection = liteDatabase.GetCollection <Film>();
                    var result     = collection.Count(Query.And(Query.EQ("ExternalId", request.ExternalId), Query.EQ("ExternalSource", request.ExternalSource.ToString())));
                    if (result > 0)
                    {
                        throw new ValidationException(
                                  $"There is allready a film with the reference {request.ExternalId} for source {request.ExternalSource}");
                    }
                }
            }

            return(Task.CompletedTask);
        }
        public Task Handle(RemoveDuplicates message, CancellationToken cancellationToken)
        {
            using (var liteDatabase = _liteDbProvider.GetDatabase())
            {
                var collection = liteDatabase.GetCollection <Film>();
                var allSources = collection.Find(x => x.ExternalSource != null).Select(x => new { x.Id, x.ExternalId, x.ExternalSource })
                                 .GroupBy(x => new { x.ExternalId, x.ExternalSource })
                                 .Where(x => x.Count() > 1)
                                 .SelectMany(x => x.Skip(1).Select(y => new BsonValue(y.Id)));

                foreach (var allSource in allSources)
                {
                    collection.Delete(allSource);
                }

                return(Task.CompletedTask);
            }
        }
示例#10
0
        public async Task Handle(AddFilmToStorageCommand message, CancellationToken cancellationToken)
        {
            using (var database = _provider.GetDatabase())
            {
                var filmsCollection   = database.GetCollection <Film>();
                var storageCollection = database.GetCollection <Storage>();

                var storage = storageCollection.FindById(message.StorageId);
                if (storage == null)
                {
                    throw new InvalidOperationException($"Could not find storage with id {message.StorageId}");
                }

                var film = filmsCollection.FindById(message.FilmId);

                if (film == null)
                {
                    throw new InvalidOperationException($"Could not find film with id {message.FilmId}");
                }

                film.Storages.Add(storage);
                filmsCollection.Update(film);
            }
        }