public async Task <UseCaseResult> Handle([NotNull] NewLogbookEntry request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            logger.LogInformation("Creating new LogbookEntry for [{title}]", request.Title);

            PhotoAndThumbnailIdentifiers teaserIdentifiers = null;

            if (request.TeaserImage != null && request.TeaserImageContentType != null)
            {
                logger.LogInformation("Storing teaser image in photo store: [{filename}]", request.TeaserImageFileName);

                teaserIdentifiers = await photoService.AddPhotoAsync(
                    PhotoCategory.LogbookTeaser,
                    request.TeaserImage,
                    request.TeaserImageContentType,
                    request.TeaserImageFileName);

                logger.LogInformation("Teaser image stored in photo store: [{filename}]", request.TeaserImageFileName);
            }

            var currentDiver = await currentUser.GetCurrentDiverAsync();

            if (currentDiver == null)
            {
                logger.LogError("Could not get diver for current user!");
                return(UseCaseResult.Fail());
            }

            var logbookEntry = new LogbookEntry(
                request.Title,
                request.Teaser,
                request.Text,
                request.IsFavorite,
                currentDiver.Id,
                request.ExternalPhotoAlbumUrl,
                request.RelatedEventId,
                teaserIdentifiers);

            var newLogbookEntry = await repository.InsertAsync(logbookEntry);

            logger.LogInformation("New LogbookEntry for [{title}] created with Id [{id}]", request.Title, newLogbookEntry.Id);
            return(UseCaseResult.Success());
        }
        public void Edit(
            [NotNull] string title,
            [NotNull] string teaser,
            [NotNull] string text,
            bool isFavorite,
            Guid editorDiverId,
            [CanBeNull] string externalPhotoAlbumUrl,
            Guid?relatedEventId,
            [CanBeNull] PhotoAndThumbnailIdentifiers photoIdentifiers)
        {
            if (string.IsNullOrWhiteSpace(title))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(title));
            }
            if (string.IsNullOrWhiteSpace(teaser))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(teaser));
            }
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(text));
            }

            Title                 = title;
            TeaserText            = teaser;
            Text                  = text;
            IsFavorite            = isFavorite;
            EditorAuthorId        = editorDiverId;
            ModifiedAt            = DateTimeOffset.UtcNow.DateTime;
            ExternalPhotoAlbumUrl = externalPhotoAlbumUrl;
            EventId               = relatedEventId;
            if (photoIdentifiers != null && (photoIdentifiers.OriginalPhotoIdentifier != null ||
                                             photoIdentifiers.ThumbnailPhotoIdentifier != null))
            {
                TeaserImage      = photoIdentifiers.OriginalPhotoIdentifier?.Serialze();
                TeaserImageThumb = photoIdentifiers.ThumbnailPhotoIdentifier?.Serialze();
            }

            RaiseDomainEvent(new LogbookEntryEditedEvent(Id, Title));
        }
        public LogbookEntry(
            [NotNull] string title,
            [NotNull] string teaser,
            [NotNull] string text,
            bool isFavorite,
            Guid authorDiverId,
            [CanBeNull] string externalPhotoAlbumUrl,
            [CanBeNull] Guid?relatedEventId,
            [CanBeNull] PhotoAndThumbnailIdentifiers photoIdentifiers)
        {
            if (string.IsNullOrWhiteSpace(title))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(title));
            }
            if (string.IsNullOrWhiteSpace(teaser))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(teaser));
            }
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(text));
            }

            Id                    = Guid.NewGuid();
            Title                 = title;
            TeaserText            = teaser;
            Text                  = text;
            IsFavorite            = isFavorite;
            IsPublished           = false;
            OriginalAuthorId      = authorDiverId;
            CreatedAt             = DateTimeOffset.Now.UtcDateTime;
            ExternalPhotoAlbumUrl = externalPhotoAlbumUrl;
            EventId               = relatedEventId;
            TeaserImage           = photoIdentifiers?.OriginalPhotoIdentifier?.Serialze();
            TeaserImageThumb      = photoIdentifiers?.ThumbnailPhotoIdentifier?.Serialze();

            RaiseDomainEvent(new LogbookEntryCreatedEvent(Id, title));
        }
Exemple #4
0
        public async Task <PhotoAndThumbnailIdentifiers> UpdatePhotoAsync(
            PhotoAndThumbnailIdentifiers existingPhoto,
            PhotoCategory category,
            Stream photoData,
            string filename,
            string contentType)
        {
            if (photoData == null)
            {
                throw new ArgumentNullException(nameof(photoData));
            }

            if (existingPhoto != null)
            {
                await RemovePhotosAsync(new[]
                {
                    existingPhoto.OriginalPhotoIdentifier,
                    existingPhoto.ThumbnailPhotoIdentifier,
                });
            }

            return(await AddPhotoWithThumbnail(category, photoData, filename, contentType));
        }
        public async Task <UseCaseResult> Handle([NotNull] EditLogbookEntry request,
                                                 CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            logger.LogInformation("Editing existing LogbookEntry with Id [{id}] and title [{title}]",
                                  request.LogbookEntryId, request.Title);

            var existingLogbookEntry = await repository.FindByIdAsync(request.LogbookEntryId);

            if (existingLogbookEntry == null)
            {
                logger.LogError("Error editing logbook-entry because the entry with with ID [{}] can not be found!",
                                request.LogbookEntryId);
                return(UseCaseResult.NotFound());
            }

            PhotoAndThumbnailIdentifiers teaserIdentifiers = null;

            if (request.TeaserImage != null && request.TeaserImageContentType != null)
            {
                logger.LogInformation("Storing teaser image in photo store: [{filename}]", request.TeaserImageFileName);

                teaserIdentifiers = await photoService.AddPhotoAsync(
                    PhotoCategory.LogbookTeaser,
                    request.TeaserImage,
                    request.TeaserImageContentType,
                    request.TeaserImageFileName);

                logger.LogInformation("Teaser image stored in photo store: [{filename}]", request.TeaserImageFileName);
            }

            var currentDiver = await currentUser.GetCurrentDiverAsync();

            if (currentDiver == null)
            {
                logger.LogError("Could not find diver for current user!");
                return(UseCaseResult.Fail());
            }

            existingLogbookEntry.Edit(
                request.Title,
                request.Teaser,
                request.Text,
                request.IsFavorite,
                currentDiver.Id,
                request.ExternalPhotoAlbumUrl,
                request.RelatedEventId,
                teaserIdentifiers);

            await repository.UpdateAsync(existingLogbookEntry);

            logger.LogInformation(
                "LogbookEntry with ID [{id}] and title [{title}] editeted successfully.",
                existingLogbookEntry.Id, existingLogbookEntry.Title);

            return(UseCaseResult.Success());
        }