Пример #1
0
 public HashtagModel FromDbItem(Dictionary <string, AttributeValue> input)
 {
     return(new HashtagModel
     {
         Hashtag = input.GetValue(FieldMappings.Hashtag.HastagId, value => $"#{value.Split('|')[1]}"),
         PhotoId = input.GetValue(FieldMappings.Hashtag.PhotoId, value => PhotoId.FromDbValue(value)),
         CreatedTime = input.GetDateTimeOffset(FieldMappings.Hashtag.CreatedTime)
     });
 }
Пример #2
0
 public PhotoLikeRecord FromDbItem(Dictionary <string, AttributeValue> input)
 {
     return(new PhotoLikeRecord
     {
         UserId = input.GetValue(FieldMappings.PhotoLike.UserId, value => UserId.FromDbValue(value)),
         PhotoId = input.GetValue(FieldMappings.PartitionKey, value => PhotoId.FromDbValue(value)),
         CreatedTime = input.GetDateTimeOffset(FieldMappings.PhotoLike.CreatedTime)
     });
 }
        private IEnumerable <PhotoWithCommentsAndLikes> ParsePhotosWithCommentsAndLikes(IOrderedEnumerable <Dictionary <string, AttributeValue> > sortedResult)
        {
            PhotoId latestPhotoId = Guid.Empty;
            List <PhotoWithCommentsAndLikes> result = new List <PhotoWithCommentsAndLikes>();

            PhotoWithCommentsAndLikes currentResult        = null;
            List <PhotoComment>       currentPhotoComments = null;
            List <PhotoLikeRecord>    currentPhotoLikes    = null;

            foreach (var item in sortedResult)
            {
                var    photoId    = PhotoId.FromDbValue(item[FieldMappings.PartitionKey].S);
                string recordType = item[FieldMappings.RecordType].S;


                if (latestPhotoId != photoId)
                {
                    if (currentResult != null)
                    {
                        result.Add(currentResult);
                    }

                    currentResult          = new PhotoWithCommentsAndLikes();
                    currentResult.Comments = (currentPhotoComments = new List <PhotoComment>());
                    currentResult.Likes    = (currentPhotoLikes = new List <PhotoLikeRecord>());

                    latestPhotoId = photoId;
                }

                switch (recordType.ToLowerInvariant())
                {
                case "photo":
                    currentResult.Photo = Mappers.PhotoModel.FromDbItem(item);
                    break;

                case "photocomment":
                    currentPhotoComments.Add(Mappers.PhotoComment.FromDbItem(item));
                    break;

                case "photolike":
                    currentPhotoLikes.Add(Mappers.PhotoLike.FromDbItem(item));
                    break;

                default:
                    logWriter.LogWarning($"RecordType '{recordType}' was not expected in {nameof(ParsePhotosWithCommentsAndLikes)}");
                    break;
                }
            }

            if (currentResult != null)
            {
                result.Add(currentResult);
            }

            return(result);
        }
Пример #4
0
        public PhotoModel FromDbItem(Dictionary <string, AttributeValue> input)
        {
            PhotoId photoId = input.TryGetValue(FieldMappings.Photo.PhotoId, out var photoIdValue)
                    ? PhotoId.FromDbValue(photoIdValue.S)
                    : (PhotoId)Guid.Empty;

            var result = new PhotoModel
            {
                CreatedTime  = input.GetDateTimeOffset(FieldMappings.Photo.CreatedTime),
                PhotoId      = photoId,
                ObjectKey    = input.GetString(FieldMappings.Photo.ObjectKey),
                State        = input.GetValue(FieldMappings.Photo.State, value => (PhotoState)Enum.Parse(typeof(PhotoState), value)),
                UserId       = input.GetValue(FieldMappings.Photo.UserId, value => UserId.FromDbValue(value)),
                UserName     = input.GetString(FieldMappings.Photo.UserName),
                LikeCount    = input.GetInt32(FieldMappings.Photo.LikeCount),
                CommentCount = input.GetInt32(FieldMappings.Photo.CommentCount),
                Hashtags     = input.GetList(FieldMappings.Photo.Hashtags, value => new HashtagModel {
                    PhotoId = photoId, Hashtag = value
                })
            };

            if (input.TryGetValue(FieldMappings.Photo.RawText, out var rawCommentValue))
            {
                result.RawText = rawCommentValue.S;
            }

            if (input.TryGetValue(FieldMappings.Photo.Score, out var scoreValue))
            {
                result.Score = double.Parse(scoreValue.N);
            }

            if (input.TryGetValue(FieldMappings.Photo.Sizes, out var sizeValues))
            {
                result.Sizes = sizeValues.L.Select(value =>
                {
                    int width;
                    int height;

                    if (!value.M.TryGetValue("Width", out var widthValue) || !int.TryParse(widthValue.S, out width))
                    {
                        throw new Exception($"Failed to parse '{widthValue.S}' as a Size Width");
                    }

                    if (!value.M.TryGetValue("Height", out var heightValue) || !int.TryParse(heightValue.S, out height))
                    {
                        throw new Exception($"Failed to parse '{heightValue.S}' as a Size Height");
                    }

                    return(new Size(width, height));
                });
            }

            return(result);
        }
        private async Task <IEnumerable <PhotoId> > GetTouchedPhotoIds(DateTimeOffset dateTimeOffset)
        {
            logWriter.LogInformation($"{nameof(GetTouchedPhotoIds)}({nameof(dateTimeOffset)} = '{dateTimeOffset.ToString(Constants.DateTimeFormatWithMilliseconds)}')");

            ScanRequest query = new ScanRequest(tableName)
            {
                // 'photolike', 'photocomment', 'photo'
                IndexName                 = "GSI2",
                FilterExpression          = $"(RecordType = :photolike OR RecordType = :photocomment OR RecordType = :photo) AND CreatedTime > :createdTime",
                ExpressionAttributeValues = new Dictionary <string, AttributeValue>
                {
                    { ":photolike", new AttributeValue("photolike") },
                    { ":photocomment", new AttributeValue("photocomment") },
                    { ":photo", new AttributeValue("photo") },
                    { ":createdTime", new AttributeValue(dateTimeOffset.ToString(Constants.DateTimeFormatWithMilliseconds)) },
                },
                ReturnConsumedCapacity = ReturnConsumedCapacity.INDEXES
            };

            var response = await dynamoDbCore.Scan(query, Mappers.Noop);

            return(response.Select(x => PhotoId.FromDbValue(x[FieldMappings.PartitionKey].S)).Distinct());
        }
        private async Task AddLikeDataForUser(UserId currentUserId, ConcurrentDictionary <PhotoId, PhotoModel> result)
        {
            if (currentUserId != (UserId)Guid.Empty)
            {
                var request = new QueryRequest
                {
                    TableName = tableName,
                    IndexName = "GSI1",
                    KeyConditionExpression    = $"{FieldMappings.Gsi1PartitionKey} = :userId and {FieldMappings.SortKey} = :userlike",
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>
                    {
                        { ":userId", new AttributeValue {
                              S = currentUserId.ToDbValue()
                          } },
                        { ":userlike", new AttributeValue {
                              S = $"like|{currentUserId.ToDbValue()}"
                          } }
                    },
                    ProjectionExpression = FieldMappings.PartitionKey
                };

                var likedPhotoIds = (await dynamoDbCore.Query(request, Mappers.Noop)).Select(x => PhotoId.FromDbValue(x[FieldMappings.PartitionKey].S));

                foreach (var photoId in likedPhotoIds)
                {
                    if (result.TryGetValue(photoId, out var photo))
                    {
                        photo.PhotoIsLikedByCurrentUser = true;
                    }
                }
            }
        }
Пример #7
0
 public PhotoComment FromDbItem(Dictionary <string, AttributeValue> input)
 {
     return(new PhotoComment
     {
         UserId = input.GetValue(FieldMappings.PhotoComment.UserId, value => UserId.FromDbValue(value)),
         PhotoId = input.GetValue(FieldMappings.PhotoComment.PhotoId, value => PhotoId.FromDbValue(value)),
         UserName = input.GetString(FieldMappings.PhotoComment.UserName),
         CreatedTime = input.GetDateTimeOffset(FieldMappings.PhotoComment.CreatedTime),
         Text = input.GetString(FieldMappings.PhotoComment.Text)
     });
 }