public static async Task <(bool, string)> RegisterProject(
            string url,
            int projectNumber,
            ProjectStudentTable projectTable,
            int userId,
            SqlDefaultDatabase db,
            CustomGroupTable groupTable,
            IStObjMap stObjMap,
            ProjectUrlTable projectUrlTable
            )
        // given url, we register the project if he can be downloaded and parsed
        {
            string  tomlString;
            Project toml;
            Type    projectType;

            try    // get the type of the project
            {
                projectType = new Type[] { typeof(ProjectPi), typeof(ProjectPfh) }[projectNumber];
            }
            catch
            {
                return(false, "Le numéro pour le type de projet n'est pas bon.");
            }

            try    // to get the ressource
            {
                tomlString = GetOnlineToml(GetTomlFromGoogleDrive.GetUrlRessource(url));
            }
            catch
            {
                return(false, "La ressource n'est pas trouvée, peut-être que le liens n'est pas le bon.");
            }

            try    // parse the toml
            {
                var method = typeof(TomlHelpers).GetMethod("GetInstanceFromToml");
                method = method.MakeGenericMethod(projectType);
                toml   = (Project)method.Invoke(null, new object[] { tomlString });
            }
            catch (Exception e)
            {
                List <string> foo = e.ToString().Split("-->")[1].Split(":").Skip(1).Take(2).ToList();
                foo[1] = foo[1].Split(".")[0];

                return(false, "Impossible de parser le fichier toml, est-ce que le fichier est correctement formaté ?\n" + String.Join(": ", foo));
            }

            (bool isValid, string message) = toml.isValid();
            if (!isValid)               // check if we got fild we want
            {
                return(false, message); // "There is missing or bad field in the toml file");
            }

            toml.logo.url = GetTomlFromGoogleDrive.GetUrlRessource(toml.logo.url);


            if (toml.team.leader != "None" && !await toml.team.isMailExisting(toml.team.leader, stObjMap))
            {
                return(false, "L'e-mail du chef de projet est invalide.");
            }
            foreach (string mail in toml.team.members)
            {
                if (!await toml.team.isMailExisting(mail, stObjMap))
                {
                    return(false, "L'un des e-mails des membres est invalide.");
                }
            }
            ;
            try    // register the project in the bdd
            {
                await RegisterProjectInBDD.SaveProject(projectType, toml, userId, db, projectTable, groupTable, projectUrlTable);
            }
            catch (Exception e)
            {
                return(false, "Impossible de sauvegarder le projet dans la BDD.");
            }

            return(true, "Le projet a été sauvegarde avec succès.");
        }
Beispiel #2
0
        public static async Task <(UserQueries, TimedUserQueries, ProjectStudentStruct, TimedUserData)> SaveProjectPfh(
            ProjectPfh project,
            ProjectStudentTable projectTable,
            int userId,
            SqlDefaultDatabase db,
            SqlStandardCallContext ctx,
            string type,
            ProjectUrlTable projectUrlTable,
            CustomGroupTable CustomGroupTable
            )
        {
            GroupQueries        groupQueries       = new GroupQueries(ctx, db);
            TraitContextQueries traitContext       = new TraitContextQueries(ctx, db);
            TimedUserQueries    timedUserQueries   = new TimedUserQueries(ctx, db);
            TimedPeriodQueries  timedPeriodQueries = new TimedPeriodQueries(ctx, db);
            UserQueries         userQueries        = new UserQueries(ctx, db);

            GroupData school = await groupQueries.GetIdSchoolByConnectUser(userId);

            // PeriodData timePeriod = await timedPeriodQueries.GetLastPeriodBySchool(school.ZoneId);
            TimedUserData timedUser = await timedUserQueries.GetTimedUser(userId, school.ZoneId);

            int traitContextId = await traitContext.GetTraitContextId(type);

            string email = project.team.leader;
            int    leaderId;

            if (email == "None")
            {
                leaderId = 0;
            }
            else
            {
                UserData user = await userQueries.GetUserByEmail(email);

                TimedUserData timedLeader = await timedUserQueries.GetTimedUser(user.UserId, school.ZoneId);

                leaderId = user.UserId;
            }

            project.background.image = GetTomlFromGoogleDrive.GetUrlRessource(project.background.image);

            ProjectStudentStruct ProjectCreate = await projectTable.CreateProjectStudent(
                ctx,
                userId,
                school.ZoneId,
                project.name.project_name,
                traitContextId,
                project.logo.url,
                project.slogan.slogan,
                project.pitch.pitch,
                leaderId,
                type,
                project.background.image
                );

            foreach (var item in project.semester.semester)
            {
                string semester   = "S0" + item.ToString();
                int    idSemester = await groupQueries.GetSpecificIdGroupByZoneIdAndGroupName(school.ZoneId, semester);

                await CustomGroupTable.AddUserAsync(ctx, userId, idSemester, ProjectCreate.ProjectStudentId, true);
            }


            return(userQueries, timedUserQueries, ProjectCreate, timedUser);
        }