private async Task <Droplet> CreateDropletRecord(int userId, Droplet droplet, Size size, List <Tag> tags, List <Region> regions)
        {
            if (!_dbContext.Sizes.Any(x => x.Slug == size.Slug))
            {
                size.CreatedByUserId = userId;
                size.UpdatedByUserId = userId;
                await size.Create(_dbContext);
            }
            else
            {
                size = _dbContext.Sizes.First(x => x.Slug == size.Slug);
            }

            foreach (var region in regions)
            {
                int reginonId = 0;
                if (!_dbContext.Regions.Any(x => x.Name == region.Name))
                {
                    await region.Create(_dbContext);

                    reginonId = region.Id;
                }
                else
                {
                    reginonId = _dbContext.Regions.First(x => x.Name == region.Name).Id;
                }
                var sr = new SizeRegion()
                {
                    SizeId = size.Id, RegionId = reginonId, CreatedByUserId = userId, UpdatedByUserId = userId
                };
                await sr.Create(_dbContext);
            }

            droplet.Sizeid          = size.Id;
            droplet.CreatedByUserId = userId;
            droplet.UpdatedByUserId = userId;
            await droplet.Create(_dbContext);

            foreach (var tag in tags)
            {
                var dt = new DropletTag()
                {
                    DropletId = droplet.Id, TagId = tag.Id, CreatedByUserId = userId, UpdatedByUserId = userId
                };
                await dt.Create(_dbContext);
            }

            return(droplet);
        }
Beispiel #2
0
        private async Task <Droplet> CreateDropletRecord(int userId, Droplet apiDroplet, Size apiSize, List <Tag> apiTags, List <Region> apiRegions)
        {
            using (var tran = _dbContext.Database.BeginTransaction())
            {
                apiSize.CreatedByUserId = userId;
                await apiSize.Create(_dbContext);

                foreach (var region in apiRegions)
                {
                    var sr = new SizeRegion()
                    {
                        Size = apiSize, Region = region, CreatedByUserId = userId
                    };
                    apiSize.SizeRegions.Add(sr);
                    await sr.Create(_dbContext);
                }

                apiDroplet.Size            = apiSize;
                apiDroplet.CreatedByUserId = userId;
                await apiDroplet.Create(_dbContext);

                foreach (var tag in apiTags)
                {
                    var dt = new DropletTag()
                    {
                        Droplet = apiDroplet, Tag = tag, CreatedByUserId = userId
                    };
                    apiDroplet.DropletTags.Add(dt);
                    await dt.Create(_dbContext);
                }

                await tran.CommitAsync();

                return(apiDroplet);
            }
        }