コード例 #1
0
        public async Task <PhotoModel> GetPhotoById(PhotoId photoId, UserId currentUserId)
        {
            PhotoModel result = null;
            bool       photoIsLikedByCurrentUser = false;

            logWriter.LogInformation($"{nameof(GetPhotoById)}({nameof(photoId)} = '{photoId}')");
            var request = new BatchGetItemRequest
            {
                RequestItems = new Dictionary <string, KeysAndAttributes>
                {
                    {
                        tableName,
                        new KeysAndAttributes
                        {
                            Keys = new List <Dictionary <string, AttributeValue> >
                            {
                                Mappers.PhotoModel.ToDbKey(new PhotoModel {
                                    PhotoId = photoId
                                }),
                                Mappers.PhotoLike.ToDbKey(new PhotoLikeRecord
                                {
                                    PhotoId = photoId,
                                    UserId  = currentUserId
                                })
                            }
                        }
                    }
                }
            };

            logWriter.LogInformation($"Batch get keys:\n{JsonConvert.SerializeObject(request)}");

            try
            {
                var getItemResponse = await dynamoDbCore.BatchGetItem(request);

                foreach (var item in getItemResponse.SelectMany(r => r.Value))
                {
                    logWriter.LogInformation($"Record type: {item[FieldMappings.RecordType].S}");
                    switch (item[FieldMappings.RecordType].S.ToLowerInvariant())
                    {
                    case "photo":
                        result = Mappers.PhotoModel.FromDbItem(item);
                        break;

                    case "photolike":
                        photoIsLikedByCurrentUser = true;
                        break;
                    }
                }

                if (result != null)
                {
                    result.PhotoIsLikedByCurrentUser = photoIsLikedByCurrentUser;
                }
                return(result);
            }
            catch (Exception ex)
            {
                logWriter.LogError(ex, $"{nameof(GetPhotoById)}({nameof(photoId)} = '{photoId}'):\n{ex.ToString()}");
                throw;
            }
        }
コード例 #2
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;
            }
        }