Esempio n. 1
0
        public async Task <ActionResult <Lecturer> > GetLecturerWithID(int lecturerID)
        {
            if (Validation.IsAnyInputObjectDataNotSpecified(new List <object> {
                lecturerID
            }))
            {
                return(BadRequest("Error: lecturer ID must be filled (with non-zero value)"));
            }

            using (var connection = new SqlConnection(Configuration.GetConnectionString("DefaultConnection")))
            {
                try
                {
                    connection.Open();

                    var lecturer = await LecturersRepository.GetDefiniteLecturer(connection, lecturerID);

                    if (lecturer == null)
                    {
                        return(NotFound("There isn't lecturer with such ID in database"));
                    }

                    return(lecturer);
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> DeleteLecturerWithID(int lecturerID)
        {
            if (Validation.IsAnyInputObjectDataNotSpecified(new List <object> {
                lecturerID
            }))
            {
                return(BadRequest("Error: lecturer ID must be specified (with non-zero value)"));
            }

            using (var connection = new SqlConnection(Configuration.GetConnectionString("DefaultConnection")))
            {
                try
                {
                    connection.Open();

                    var numberOfAffectedRows = await LecturersRepository.DeleteLecturerAsync(connection, lecturerID);

                    if (numberOfAffectedRows == 0)
                    {
                        return(NotFound("Lecturer with such id isn't found in database"));
                    }
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }

            return(NoContent());
        }
Esempio n. 3
0
        public async Task <ActionResult <List <Lecturer> > > GetAllLecturers()
        {
            using (var connection = new SqlConnection(Configuration.GetConnectionString("DefaultConnection")))
            {
                try
                {
                    connection.Open();

                    return(await LecturersRepository.GetAllLecturersAsync(connection));
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> Put(int lecturerID, [FromBody] Dictionary <string, string> lecturer)
        {
            if (Validation.IsAnyInputObjectDataNotSpecified(new List <object> {
                lecturerID, lecturer.ContainsKey("Name") ? lecturer["Name"] : null,
                lecturer.ContainsKey("BirthDate") ? lecturer["BirthDate"] : null
            }))
            {
                return(BadRequest("Error: all lecturer input data must be specified"));
            }

            lecturer["Name"]      = lecturer["Name"].Trim();
            lecturer["BirthDate"] = lecturer["BirthDate"].Trim();

            if (!Validation.IsValidFirstOrLastName(lecturer["Name"]))
            {
                return(BadRequest("Error: name is incorrect"));
            }

            if (!Validation.ValidateDateTimeAndGetParsed(lecturer["BirthDate"], out DateTime resultBirthDate))
            {
                return(BadRequest("Error: birth date is incorrect"));
            }

            var resultLecturer = new Lecturer
            {
                LecturerID = lecturerID,
                Name       = lecturer["Name"],
                BirthDate  = resultBirthDate
            };

            using (var connection = new SqlConnection(Configuration.GetConnectionString("DefaultConnection")))
            {
                try
                {
                    connection.Open();

                    await LecturersRepository.UpdateLecturerAsync(connection, resultLecturer);
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }

            return(NoContent());
        }
Esempio n. 5
0
        public async Task <IActionResult> DeleteAllLecturers()
        {
            using (var connection = new SqlConnection(Configuration.GetConnectionString("DefaultConnection")))
            {
                try
                {
                    connection.Open();

                    var numberOfAffectedRows = await LecturersRepository.DeleteAllLecturersAsync(connection);

                    if (numberOfAffectedRows == 0)
                    {
                        return(NotFound("There aren't any lecturers in database"));
                    }
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }

            return(NoContent());
        }