コード例 #1
0
                static async Task AddLayoutStationAsync(ModulesDbContext dbContext, int participantId, int layoutId, Module module)
                {
                    if (module.StationId.HasValue)
                    {
                        var existing = await dbContext.LayoutStations.SingleOrDefaultAsync(ls => ls.LayoutId == layoutId && ls.StationId == module.StationId);

                        if (existing is null)
                        {
                            var addedStation = new LayoutStation {
                                LayoutId = layoutId, StationId = module.StationId.Value
                            };
                            dbContext.LayoutStations.Add(addedStation);
                        }
                    }
                }
    /// <summary>
    /// Adds <see cref="LayoutModule">modules</see> and <see cref="LayoutService">stations</see> in a <see cref="ModulePackage"/> to a <see cref="Layout"/>
    /// </summary>
    /// <param name="principal"></param>
    /// <param name="participantId"></param>
    /// <param name="layoutId"></param>
    /// <param name="package"></param>
    /// <returns></returns>
    public async Task <(int Count, string Message)> AddPackageModulesAsync(ClaimsPrincipal?principal, int layoutParticipantId, ModulePackage package)
    {
        if (principal.IsAuthenticated())
        {
            var result = package.Modules.Count();
            using var dbContext = Factory.CreateDbContext();
            var participant = await dbContext.LayoutParticipants
                              .SingleOrDefaultAsync(lp => lp.Id == layoutParticipantId);

            if (participant is null)
            {
                return(-1, Resources.Strings.NotFound);
            }
            foreach (var module in package.Modules)
            {
                var existing = await dbContext.LayoutModules.SingleOrDefaultAsync(lm => lm.ModuleId == module.Id && lm.LayoutParticipantId == layoutParticipantId);

                if (existing is null)
                {
                    var layoutModule = new LayoutModule {
                        ModuleId = module.Id, LayoutParticipantId = participant.Id, RegisteredTime = TimeProvider.Now
                    };
                    if (module.StationId.HasValue)
                    {
                        var layoutStation = new LayoutStation {
                            StationId = module.StationId.Value
                        };
                        layoutModule.LayoutStation = layoutStation;
                    }
                    dbContext.LayoutModules.Add(layoutModule);
                    await dbContext.SaveChangesAsync();
                }
                else
                {
                    result--;
                }
            }
            return(result == 0 ? (-1, Resources.Strings.NoModification) : (1, Resources.Strings.Saved));
        }
        return(0, Resources.Strings.NotAuthorized);
    }