示例#1
0
        public async Task <bool> Remove(ShopAggregate shop)
        {
            if (shop == null)
            {
                throw new ArgumentNullException(nameof(shop));
            }

            var record = await _context.Shops.FirstOrDefaultAsync(s => s.Id == shop.Id);

            if (record == null)
            {
                return(false);
            }

            try
            {
                _context.Shops.Remove(record);
                await _context.SaveChangesAsync();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
示例#2
0
        public static Shop ToModel(this ShopAggregate shop)
        {
            if (shop == null)
            {
                throw new ArgumentNullException(nameof(shop));
            }

            return(new Shop
            {
                Id = shop.Id,
                Subject = shop.Subject,
                Name = shop.Name,
                Description = shop.Description,
                BannerImage = shop.BannerImage,
                ProfileImage = shop.ProfileImage,
                CategoryId = shop.CategoryId,
                StreetAddress = shop.StreetAddress,
                PostalCode = shop.PostalCode,
                Locality = shop.Locality,
                Country = shop.Country,
                CreateDateTime = shop.CreateDateTime,
                UpdateDateTime = shop.UpdateDateTime,
                GooglePlaceId = shop.GooglePlaceId,
                Latitude = shop.Latitude,
                Longitude = shop.Longitude,
                TotalScore = shop.TotalScore
            });
        }
示例#3
0
        public void Save(ShopAggregate aggregate)
        {
            var saleItem = aggregate.GetSale();

            using (var db = new YemekSepetiDbContext())
            {
                db.SaleSet.Add(saleItem);
                db.SaveChanges();
            }
        }
示例#4
0
        public async Task <AddCommentValidationResult> Validate(AddShopCommentCommand comment)
        {
            if (comment == null)
            {
                throw new ArgumentNullException(nameof(comment));
            }

            var validationResult = Validate(comment.ShopId, comment.Content, comment.Score, Constants.DtoNames.Comment.ShopId);

            if (!validationResult.IsValid)
            {
                return(validationResult);
            }

            ShopAggregate shop = null;

            // Check shop exists && 1 comment.
            if (!string.IsNullOrWhiteSpace(comment.ShopId))
            {
                shop = await _shopRepository.Get(comment.ShopId);

                if (shop == null)
                {
                    return(new AddCommentValidationResult(ErrorDescriptions.TheShopDoesntExist));
                }

                var comments = await _shopRepository.SearchComments(new SearchShopCommentsParameter
                {
                    ShopId  = comment.ShopId,
                    Subject = comment.Subject
                });

                if (comments.TotalResults > 0)
                {
                    return(new AddCommentValidationResult(ErrorDescriptions.TheCommentAlreadyExists));
                }
            }

            // Check content size.
            if (!string.IsNullOrWhiteSpace(comment.Content) && comment.Content.Length > 255)
            {
                return(new AddCommentValidationResult(string.Format(ErrorDescriptions.TheParameterLengthCannotExceedNbCharacters, Constants.DtoNames.Comment.Content, "255")));
            }

            // Check score.
            if (comment.Score < 1 || comment.Score > 5)
            {
                return(new AddCommentValidationResult(ErrorDescriptions.TheScoreMustBeBetween));
            }

            return(new AddCommentValidationResult());
        }
示例#5
0
        public void Checkout(string userid, List <string> cartIdList)
        {
            var aggregate = new ShopAggregate();

            List <Cart> cartList   = _shopRepository.GetCartListByIdList(cartIdList);
            var         mealIdList = cartList.Select(a => a.MealID).ToList();

            List <Meal> mealList = _mealRepository.GetMealListByIdList(mealIdList);

            aggregate.Checkout(cartList, mealList);

            string payCode = _checkoutService.PayCheckout(aggregate.GetTotalPrice());

            aggregate.SetSale(payCode, userid);

            _shopRepository.Save(aggregate);
        }
示例#6
0
        public void Enrich(IHalResponseBuilder halResponseBuilder, ShopAggregate shop)
        {
            halResponseBuilder.AddEmbedded(e => e.AddObject(_responseBuilder.GetShop(shop),
                                                            (l) =>
            {
                l.AddOtherItem("category", new Dtos.Link("/" + Constants.RouteNames.ShopCategories + "/" + shop.CategoryId, shop.ShopCategory.Name)).AddSelf(Constants.RouteNames.Shops + "/" + shop.Id);
                if (shop.ShopFilters != null && shop.ShopFilters.Any())
                {
                    foreach (var filter in shop.ShopFilters)
                    {
                        l.AddOtherItem("filters", new Dtos.Link("/" + Constants.RouteNames.Filers + "/" + filter.Id, filter.Name));
                    }
                }

                if (shop.ProductCategories != null && shop.ProductCategories.Any())
                {
                    foreach (var productCategory in shop.ProductCategories)
                    {
                        l.AddOtherItem("productCategories", new Dtos.Link("/" + Constants.RouteNames.Filers + "/" + productCategory.Id, productCategory.Name));
                    }
                }
            }));
        }
示例#7
0
        public async Task <bool> Add(ShopAggregate shop)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false))
            {
                try
                {
                    var record            = shop.ToModel();
                    var tags              = new List <Models.ShopTag>();
                    var filters           = new List <Models.Filter>();
                    var productCategories = new List <Models.ProductCategory>();
                    if (shop.TagNames != null && shop.TagNames.Any()) // Add tags
                    {
                        var tagNames      = shop.TagNames;
                        var connectedTags = _context.Tags.Where(t => tagNames.Any(tn => t.Name == tn));
                        foreach (var connectedTag in connectedTags)
                        {
                            tags.Add(new Models.ShopTag
                            {
                                Id      = Guid.NewGuid().ToString(),
                                TagName = connectedTag.Name,
                                ShopId  = shop.Id
                            });
                        }

                        var connectedTagNames = (await connectedTags.Select(t => t.Name).ToListAsync().ConfigureAwait(false));
                        foreach (var notExistingTagName in tagNames.Where(tn => !connectedTagNames.Contains(tn)))
                        {
                            var newTag = new Models.Tag
                            {
                                Name        = notExistingTagName,
                                Description = notExistingTagName
                            };
                            _context.Tags.Add(newTag);
                            tags.Add(new Models.ShopTag
                            {
                                Id      = Guid.NewGuid().ToString(),
                                TagName = notExistingTagName,
                                ShopId  = shop.Id,
                                Tag     = newTag
                            });
                        }
                    }

                    if (shop.ShopFilters != null)
                    {
                        foreach (var shopFilter in shop.ShopFilters)
                        {
                            filters.Add(new Models.Filter
                            {
                                Id     = shopFilter.Id,
                                Name   = shopFilter.Name,
                                ShopId = shop.Id,
                                Values = shopFilter.Values == null ? new List <Models.FilterValue>() : shopFilter.Values.Select(v => new Models.FilterValue
                                {
                                    Id             = v.Id,
                                    Content        = v.Content,
                                    CreateDateTime = v.CreateDateTime,
                                    UpdateDateTime = v.UpdateDateTime,
                                    FilterId       = shopFilter.Id
                                }).ToList()
                            });
                        }
                    }

                    if (shop.ProductCategories != null)
                    {
                        foreach (var productCategory in shop.ProductCategories)
                        {
                            productCategories.Add(new Models.ProductCategory
                            {
                                Id             = productCategory.Id,
                                Description    = productCategory.Description,
                                Name           = productCategory.Name,
                                ShopId         = shop.Id,
                                CreateDateTime = productCategory.CreateDateTime,
                                UpdateDateTime = productCategory.UpdateDateTime
                            });
                        }
                    }

                    record.ProductCategories = productCategories;
                    record.Filters           = filters;
                    record.ShopTags          = tags;
                    _context.Shops.Add(record);
                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                    return(true);
                }
                catch
                {
                    transaction.Rollback();
                    return(false);
                }
            }
        }
示例#8
0
        public async Task <bool> Update(ShopAggregate shop)
        {
            if (shop == null)
            {
                throw new ArgumentNullException(nameof(shop));
            }

            using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false))
            {
                try
                {
                    var record = await _context.Shops
                                 .Include(s => s.Comments)
                                 .Include(s => s.ProductCategories)
                                 .Include(s => s.ShopTags)
                                 .Include(c => c.ShopGameEntities).ThenInclude(s => s.GameEntity)
                                 .Include(s => s.Filters).ThenInclude(f => f.Values)
                                 .FirstOrDefaultAsync(s => s.Id == shop.Id).ConfigureAwait(false);

                    if (record == null)
                    {
                        transaction.Rollback();
                        return(false);
                    }

                    record.BannerImage    = shop.BannerImage;
                    record.CategoryId     = shop.CategoryId;
                    record.Country        = shop.Country;
                    record.Description    = shop.Description;
                    record.GooglePlaceId  = shop.GooglePlaceId;
                    record.Latitude       = shop.Latitude;
                    record.Locality       = shop.Locality;
                    record.Longitude      = shop.Longitude;
                    record.Name           = shop.Name;
                    record.PostalCode     = shop.PostalCode;
                    record.ProfileImage   = shop.ProfileImage;
                    record.StreetAddress  = shop.StreetAddress;
                    record.Subject        = shop.Subject;
                    record.TotalScore     = shop.TotalScore;
                    record.AverageScore   = shop.AverageScore;
                    record.UpdateDateTime = shop.UpdateDateTime;

                    var tags          = new List <Models.ShopTag>(); // Update tags.
                    var shopTags      = shop.TagNames == null ? new List <string>() : shop.TagNames;
                    var tagNames      = shop.TagNames;
                    var connectedTags = _context.Tags.Where(t => tagNames.Any(tn => t.Name == tn));
                    foreach (var connectedTag in connectedTags)
                    {
                        tags.Add(new Models.ShopTag
                        {
                            Id      = Guid.NewGuid().ToString(),
                            TagName = connectedTag.Name,
                            ShopId  = shop.Id
                        });
                    }

                    var connectedTagNames = (await connectedTags.Select(t => t.Name).ToListAsync().ConfigureAwait(false));
                    foreach (var notExistingTagName in tagNames.Where(tn => !connectedTagNames.Contains(tn)))
                    {
                        var newTag = new Models.Tag
                        {
                            Name        = notExistingTagName,
                            Description = notExistingTagName
                        };
                        _context.Tags.Add(newTag);
                        tags.Add(new Models.ShopTag
                        {
                            Id      = Guid.NewGuid().ToString(),
                            TagName = notExistingTagName,
                            ShopId  = shop.Id,
                            Tag     = newTag
                        });
                    }
                    record.ShopTags = tags;

                    var productCategories         = shop.ProductCategories == null ? new List <ShopProductCategory>() : shop.ProductCategories;
                    var productCategoryIds        = productCategories.Select(p => p.Id);
                    var productCategoriesToUpdate = record.ProductCategories.Where(c => productCategoryIds.Contains(c.Id));
                    var productCategoriesToRemove = record.ProductCategories.Where(c => !productCategoryIds.Contains(c.Id));
                    var existingCategoryIds       = record.ProductCategories.Select(c => c.Id);
                    var productCategoriesToAdd    = productCategories.Where(c => !existingCategoryIds.Contains(c.Id));
                    foreach (var productCategoryToUpdate in productCategoriesToUpdate)
                    {
                        var productCategory = productCategories.First(p => p.Id == productCategoryToUpdate.Id);
                        productCategoryToUpdate.Name           = productCategory.Name;
                        productCategoryToUpdate.UpdateDateTime = productCategory.UpdateDateTime;
                        productCategoryToUpdate.Description    = productCategory.Description;
                    }

                    foreach (var productCategoryToRemove in productCategoriesToRemove)
                    {
                        _context.ProductCategories.Remove(productCategoryToRemove);
                    }

                    foreach (var productCategoryToAdd in productCategoriesToAdd)
                    {
                        var rec = new Models.ProductCategory
                        {
                            Id             = productCategoryToAdd.Id,
                            Description    = productCategoryToAdd.Description,
                            Name           = productCategoryToAdd.Name,
                            CreateDateTime = productCategoryToAdd.CreateDateTime,
                            UpdateDateTime = productCategoryToAdd.UpdateDateTime,
                            ShopId         = shop.Id
                        };
                        _context.ProductCategories.Add(rec);
                    }

                    var filters           = shop.ShopFilters == null ? new List <ShopFilter>() : shop.ShopFilters; // Update filters.
                    var filterIds         = filters.Select(c => c.Id);
                    var filtersToUpdate   = record.Filters.Where(c => filterIds.Contains(c.Id));
                    var filtersToRemove   = record.Filters.Where(c => !filterIds.Contains(c.Id));
                    var existingFilterIds = record.Filters.Select(c => c.Id);
                    var filtersToAdd      = filters.Where(c => !existingFilterIds.Contains(c.Id));
                    foreach (var filterToUpdate in filtersToUpdate)
                    {
                        var filter = filters.First(c => c.Id == filterToUpdate.Id);
                        filterToUpdate.Name = filter.Name;
                        var filterValues           = filter.Values == null ? new List <ShopFilterValue>() : filter.Values;
                        var filterValueIds         = filterValues.Select(f => f.Id);
                        var filterValuesToUpdate   = filterToUpdate.Values.Where(v => filterValueIds.Contains(v.Id)); // Update filter values.
                        var filterValuesToRemove   = filterToUpdate.Values.Where(v => !filterValueIds.Contains(v.Id));
                        var existingFilterValueIds = filterToUpdate.Values.Select(v => v.Id);
                        var filterValuesToAdd      = filter.Values.Where(v => !existingFilterValueIds.Contains(v.Id));
                        foreach (var filterValueToUpdate in filterValuesToUpdate)
                        {
                            var filterValue = filterToUpdate.Values.First(v => v.Id == filterValueToUpdate.Id);
                            filterValue.UpdateDateTime = filterValueToUpdate.UpdateDateTime;
                            filterValue.Content        = filterValueToUpdate.Content;
                        }

                        foreach (var filterValueToRemove in filterValuesToRemove)
                        {
                            _context.FilterValues.Remove(filterValueToRemove);
                        }

                        foreach (var filterValueToAdd in filterValuesToAdd)
                        {
                            var rec = new Models.FilterValue
                            {
                                Id             = filterValueToAdd.Id,
                                Content        = filterValueToAdd.Content,
                                CreateDateTime = filterValueToAdd.CreateDateTime,
                                UpdateDateTime = filterValueToAdd.UpdateDateTime,
                                FilterId       = filter.Id
                            };
                            _context.FilterValues.Add(rec);
                        }
                    }

                    foreach (var filterToRemove in filtersToRemove)
                    {
                        _context.Filters.Remove(filterToRemove);
                    }

                    foreach (var filterToAdd in filtersToAdd)
                    {
                        var rec = new Models.Filter
                        {
                            Id     = filterToAdd.Id,
                            Name   = filterToAdd.Name,
                            ShopId = shop.Id,
                            Values = filterToAdd.Values == null ? new List <Models.FilterValue>() : filterToAdd.Values.Select(v => new Models.FilterValue
                            {
                                Id             = v.Id,
                                Content        = v.Content,
                                CreateDateTime = v.CreateDateTime,
                                UpdateDateTime = v.UpdateDateTime,
                                FilterId       = filterToAdd.Id
                            }).ToList()
                        };
                        _context.Filters.Add(rec);
                    }

                    var comments           = shop.Comments == null ? new List <ShopComment>() : shop.Comments; // Update comments
                    var commentIds         = comments.Select(c => c.Id);
                    var commentsToUpdate   = record.Comments.Where(c => commentIds.Contains(c.Id));
                    var commentsToRemove   = record.Comments.Where(c => !commentIds.Contains(c.Id));
                    var existingCommentIds = record.Comments.Select(c => c.Id);
                    var commentsToAdd      = comments.Where(c => !existingCommentIds.Contains(c.Id));
                    foreach (var commentToUpdate in commentsToUpdate)
                    {
                        var comment = comments.First(c => c.Id == commentToUpdate.Id);
                        commentToUpdate.Score          = comment.Score;
                        commentToUpdate.Subject        = comment.Subject;
                        commentToUpdate.UpdateDateTime = comment.UpdateDateTime;
                        commentToUpdate.Content        = comment.Content;
                    }

                    foreach (var commentToRemove in commentsToRemove)
                    {
                        _context.Comments.Remove(commentToRemove);
                    }

                    foreach (var commentToAdd in commentsToAdd)
                    {
                        var rec = new Models.Comment
                        {
                            Id             = commentToAdd.Id,
                            Content        = commentToAdd.Content,
                            Score          = commentToAdd.Score,
                            CreateDateTime = commentToAdd.CreateDateTime,
                            ShopId         = shop.Id,
                            UpdateDateTime = commentToAdd.UpdateDateTime,
                            Subject        = commentToAdd.Subject
                        };
                        _context.Comments.Add(rec);
                    }


                    var gameEntities           = shop.ShopGameEntities == null ? new List <ShopAggregateGameEntity>() : shop.ShopGameEntities; // Update game entities.
                    var gameEntitiesId         = gameEntities.Select(c => c.Id);
                    var gameEntitiesToUpdate   = record.ShopGameEntities.Where(c => gameEntitiesId.Contains(c.Id));
                    var gameEntitiesToRemove   = record.ShopGameEntities.Where(c => !gameEntitiesId.Contains(c.Id));
                    var existingGameEntitiesId = record.ShopGameEntities.Select(c => c.Id);
                    var gameEntitiesToAdd      = gameEntities.Where(c => !existingGameEntitiesId.Contains(c.Id));
                    foreach (var gameEntityToUpdate in gameEntitiesToUpdate)
                    {
                        var gameEntity = gameEntities.First(c => c.Id == gameEntityToUpdate.Id);
                        gameEntityToUpdate.Col               = gameEntity.Col;
                        gameEntityToUpdate.Row               = gameEntity.Row;
                        gameEntityToUpdate.GameEntityId      = gameEntity.Name;
                        gameEntityToUpdate.ProductCategoryId = gameEntity.ProductCategoryId;
                        gameEntityToUpdate.IsFlipped         = gameEntity.IsFlipped;
                    }

                    foreach (var gameEntityToRemove in gameEntitiesToRemove)
                    {
                        _context.ShopGameEntities.Remove(gameEntityToRemove);
                    }

                    foreach (var gameEntityToAdd in gameEntitiesToAdd)
                    {
                        var rec = new Models.ShopGameEntity
                        {
                            Id                = gameEntityToAdd.Id,
                            Col               = gameEntityToAdd.Col,
                            Row               = gameEntityToAdd.Row,
                            GameEntityId      = gameEntityToAdd.Name,
                            ProductCategoryId = gameEntityToAdd.ProductCategoryId,
                            ShopId            = shop.Id,
                            IsFlipped         = gameEntityToAdd.IsFlipped
                        };
                        _context.ShopGameEntities.Add(rec);
                    }

                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                    return(true);
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return(false);
                }
            }
        }
示例#9
0
 public UpdateShopValidationResult(ShopAggregate shop)
 {
     Shop    = shop;
     IsValid = true;
 }
示例#10
0
        public async Task Handle(AddShopCommand message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var aggregate = new ShopAggregate
            {
                Id             = message.Id,
                Subject        = message.Subject,
                Name           = message.Name,
                Description    = message.Description,
                BannerImage    = message.BannerImage,
                ProfileImage   = message.ProfileImage,
                CategoryId     = message.CategoryId,
                StreetAddress  = message.StreetAddress,
                PostalCode     = message.PostalCode,
                Locality       = message.Locality,
                Country        = message.Country,
                GooglePlaceId  = message.GooglePlaceId,
                Longitude      = message.Longitude,
                Latitude       = message.Latitude,
                CreateDateTime = message.CreateDateTime,
                UpdateDateTime = message.UpdateDateTime,
                TagNames       = message.TagNames
            };

            if (message.ProductCategories != null)
            {
                aggregate.ProductCategories = message.ProductCategories.Select(p => new ShopProductCategory
                {
                    Id             = Guid.NewGuid().ToString(),
                    Name           = p.Name,
                    Description    = p.Description,
                    CreateDateTime = DateTime.UtcNow,
                    UpdateDateTime = DateTime.UtcNow
                }).ToList();
            }

            if (message.ProductFilters != null)
            {
                aggregate.ShopFilters = message.ProductFilters.Select(f => new ShopFilter
                {
                    Id     = Guid.NewGuid().ToString(),
                    Name   = f.Name,
                    Values = f.Values == null ? new List <ShopFilterValue>() : f.Values.Select(v => new ShopFilterValue
                    {
                        Id             = Guid.NewGuid().ToString(),
                        CreateDateTime = DateTime.UtcNow,
                        UpdateDateTime = DateTime.UtcNow,
                        Content        = v
                    })
                }).ToList();
            }

            await _shopRepository.Add(aggregate);

            _eventPublisher.Publish(new ShopAddedEvent
            {
                Subject   = aggregate.Subject,
                ShopId    = message.Id,
                Name      = message.Name,
                Longitude = message.Longitude,
                Latitude  = message.Latitude,
                CommonId  = message.CommonId
            });
        }