private async Task RemoveLikeRecord(UserId userId, PhotoId photoId)
        {
            logWriter.LogInformation($"{nameof(RemoveLikeRecord)}({nameof(userId)} = '{userId}', {nameof(photoId)} = '{photoId}')");

            var photo = await photoRepository.GetPhotoById(photoId, Guid.Empty);

            var newLikeCount = photo.LikeCount - 1;

            var deleteItemRequest = new DeleteItemRequest(
                tableName,
                Mappers.PhotoLike.ToDbKey(new PhotoLikeRecord
            {
                UserId      = userId,
                PhotoId     = photoId,
                CreatedTime = system.Time.UtcNow
            }));

            try
            {
                await dynamoDbCore.DeleteItem(deleteItemRequest);
                await UpdateLikeCount(photo, newLikeCount);
            }
            catch (Exception ex)
            {
                logWriter.LogError(ex, $"Error in {nameof(RemoveLikeRecord)}({nameof(userId)} = '{userId}', {nameof(photoId)} = '{photoId}'):\n{ex.ToString()}");
                throw;
            }
        }
        public async Task DeletePhotoComment(PhotoComment comment)
        {
            var photo = await photoRepository.GetPhotoById(comment.PhotoId, Guid.Empty);

            var deleteCommentRequest = new DeleteItemRequest
            {
                TableName = tableName,
                Key       = Mappers.PhotoComment.ToDbKey(comment)
            };

            try
            {
                await dynamoDbCore.DeleteItem(deleteCommentRequest);
                await UpdateCommentCount(photo, photo.CommentCount - 1);
            }
            catch (Exception ex)
            {
                logWriter.LogError(ex, $"Error in {nameof(DeletePhotoComment)}():\n{ex.ToString()}");
                throw;
            }
        }
Beispiel #3
0
        private async Task RemoveLikeRecord(UserId userId, PhotoId photoId)
        {
            BatchGetItemRequest request = new BatchGetItemRequest
            {
                RequestItems = new Dictionary <string, KeysAndAttributes>
                {
                    { tableName, new KeysAndAttributes {
                          Keys = new[] {
                              Mappers.PhotoModel.ToDbKey(new PhotoModel {
                                    PhotoId = photoId
                                }),
                              Mappers.PhotoLike.ToDbKey(new PhotoLikeRecord {
                                    PhotoId = photoId, UserId = userId
                                })
                          }.ToList()
                      } }
                }
            };

            var items = await dynamoDbCore.BatchGetItem(request);

            PhotoModel      photo      = null;
            PhotoLikeRecord likeRecord = null;

            foreach (var item in items[tableName])
            {
                switch (item[FieldMappings.RecordType].S.ToLower())
                {
                case "photo":
                    photo = Mappers.PhotoModel.FromDbItem(item);
                    break;

                case "photolike":
                    likeRecord = Mappers.PhotoLike.FromDbItem(item);
                    break;
                }
            }

            if (likeRecord == null)
            {
                return;
            }

            logWriter.LogInformation($"{nameof(RemoveLikeRecord)}({nameof(userId)} = '{userId}', {nameof(photoId)} = '{photoId}')");

            var scoreDelta = scoreCalculator.GetLikeScore(likeRecord) * -1;

            var deleteItemRequest = new DeleteItemRequest(
                tableName,
                Mappers.PhotoLike.ToDbKey(new PhotoLikeRecord
            {
                UserId      = userId,
                PhotoId     = photoId,
                CreatedTime = system.Time.UtcNow
            }));

            try
            {
                await dynamoDbCore.DeleteItem(deleteItemRequest);
                await UpdateLikeCountAndScore(photo, -1, scoreDelta);
            }
            catch (Exception ex)
            {
                logWriter.LogError(ex, $"Error in {nameof(RemoveLikeRecord)}({nameof(userId)} = '{userId}', {nameof(photoId)} = '{photoId}'):\n{ex.ToString()}");
                throw;
            }
        }