Пример #1
0
            public async Task <CreateLocationItemDto> Handle(CreateLocationItemCommand request, CancellationToken cancellationToken)
            {
                var entity = new Domain.Entities.Location()
                {
                    Name        = request.Name,
                    IsEnabled   = request.IsEnabled,
                    OpeningTime = request.OpeningTime,
                    ClosingTime = request.ClosingTime,
                    Description = request.Description,
                    Address     = request.Address,
                };
                await context.Locations.AddAsync(entity);


                if (request.InstructorLists.Count > 0)
                {
                    var getInstructorListDbQuery = context.Instructor
                                                   .Include(i => i.LocationInstructors)
                                                   .OrderBy(i => i.FirstName)
                                                   .Where(i => request.InstructorLists.Contains(i.Id));
                    var instructorEntities = await getInstructorListDbQuery.ToListAsync();

                    if (instructorEntities.Count != request.InstructorLists.Count)
                    {
                        throw new EntityListCountMismatchException <Domain.Entities.Instructor>(instructorEntities, request.InstructorLists);
                    }
                    foreach (var instructorEntity in instructorEntities)
                    {
                        var li = new LocationInstructor
                        {
                            Instructor = instructorEntity,
                            Location   = entity,
                        };
                        entity.LocationInstructors.Add(li);
                    }
                }

                await context.SaveChangesAsync(cancellationToken);

                return(new CreateLocationItemDto
                {
                    Id = entity.Id
                });
            }
            private async Task AddLocationListAsync(Instructor entity, ICollection <Guid> locationList)
            {
                var getLocationListDbQuery = context.Locations
                                             .Include(i => i.LocationInstructors)
                                             .OrderBy(i => i.Name)
                                             .Where(i => locationList.Contains(i.Id));
                var locationEntities = await getLocationListDbQuery.ToListAsync();

                if (locationEntities.Count != locationList.Count)
                {
                    throw new EntityListCountMismatchException <Location>(locationEntities, locationList);
                }
                foreach (var locationEntity in locationEntities)
                {
                    var ic = new LocationInstructor
                    {
                        Instructor = entity,
                        Location   = locationEntity,
                    };
                    locationEntity.LocationInstructors.Add(ic);
                }
            }