Ejemplo n.º 1
0
 public static SemesterCreate MapSemesterCreate(SemesterCreateModel source)
 {
     return(new SemesterCreate
     {
         StartDate = source.StartDate,
         AcademicYear = source.AcademicYear,
         EndDate = source.EndDate,
         IsWinter = source.IsWinter
     });
 }
Ejemplo n.º 2
0
        public IActionResult Post([FromBody] SemesterCreateModel model)
        {
            if (!this.Validator.ValidateObject(model) ||
                !this.Validator.ValidateRequiredStringProperty(model.Name) ||
                !this.Validator.ValidateDates(model.StartDate, model.EndDate))
            {
                return(BadRequest(new { message = "Bad parameters passed!" }));
            }

            foreach (var discipline in model.Disciplines)
            {
                if (!this.Validator.ValidateObject(discipline) ||
                    !this.Validator.ValidateRequiredStringProperty(discipline.DisciplineName) ||
                    !this.Validator.ValidateRequiredStringProperty(discipline.ProfessorName))
                {
                    return(BadRequest(new { message = "Bad discipline parameters passed!" }));
                }
            }

            var affectedRows = this.semesterService.Create(model);

            return(this.BuildNonQueryResponse(affectedRows));
        }
Ejemplo n.º 3
0
        public int Add(SemesterCreateModel semester)
        {
            var affectedRows = 0;
            int idOfSemester = 0;

            using (var connection = this.Context.Connection)
            {
                var statement = $"INSERT INTO {semesterTableName}(`Name`,`StartDate`,`EndDate`)VALUES(@Name,@StartDate,@EndDate);SELECT Id FROM {semesterTableName} WHERE Id = LAST_INSERT_ID();";
                var command   = new MySqlCommand(statement, connection);

                command.Parameters.AddWithValue("Name", semester.Name);
                command.Parameters.AddWithValue("StartDate", semester.StartDate);
                command.Parameters.AddWithValue("EndDate", semester.EndDate);
                try
                {
                    connection.Open();
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        idOfSemester = (int)reader["Id"];
                    }
                }
                catch (Exception ex)
                {
                    this.Log(this.GetExceptionText(ex));
                }
            }

            if (semester.Disciplines.Count > 0)
            {
                using (var connection = this.Context.Connection)
                {
                    var statement = $"INSERT INTO  {disciplineTableName} (`DisciplineName`,`ProfessorName`,`SemesterId`) VALUES ";
                    var command   = new MySqlCommand(statement, connection);

                    for (int i = 0; i < semester.Disciplines.Count; i++)
                    {
                        var disciplineForAdd = $"(@DisciplineName{i}, @ProfessorName{i}, {idOfSemester})";
                        command.Parameters.AddWithValue($"DisciplineName{i}", semester.Disciplines[i].DisciplineName);
                        command.Parameters.AddWithValue($"ProfessorName{i}", semester.Disciplines[i].ProfessorName);

                        statement = $"{statement}{disciplineForAdd}";
                        if (i == semester.Disciplines.Count - 1)
                        {
                            statement = $"{statement};";
                        }
                        else
                        {
                            statement = $"{statement},";
                        }
                    }

                    command.CommandText = statement;
                    try
                    {
                        connection.Open();
                        affectedRows = command.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        this.Log(this.GetExceptionText(ex));
                    }
                }
            }

            return(affectedRows + 1);
        }
Ejemplo n.º 4
0
        public int Create(SemesterCreateModel model)
        {
            var affectedRows = this.repository.Add(model);

            return(affectedRows);
        }