public PromotionStudentRepsonse EnrollStudentPromotions(EnrollStudentPromotions promotion) { using (var connection = new SqlConnection("Data Source=db-mssql.pjwstk.edu.pl;Initial Catalog=s16578;Integrated Security=True")) using (var command = connection.CreateCommand()) using (var transaction = new TransactionScope()) { var insertedData = new PromotionStudentRepsonse(); try { connection.Open(); command.CommandText = "EXEC PromoteStudents @Studies = @studies, @Semester = @semester"; command.Parameters.AddWithValue("@studies", promotion.Studies); command.Parameters.AddWithValue("@semester", promotion.Semester); using (var dataReader = command.ExecuteReader()) { if (dataReader.Read()) { insertedData.IdEnrollment = dataReader.GetInt32(0); insertedData.Semester = dataReader.GetInt32(2); insertedData.IdStudy = dataReader.GetInt32(1); insertedData.StartDate = dataReader.GetDateTime(3); } } } catch (SqlException sqlerror) { throw new InvalidOperationException(sqlerror.Message); } return(insertedData); } }
public IActionResult EnrollStudentPromotions(EnrollStudentPromotions promotion) { try { var enrollment = _service.EnrollStudentPromotions(promotion); var result = new ObjectResult(enrollment); result.StatusCode = (int)HttpStatusCode.Created; return(result); } catch (InvalidOperationException invalidOperation) { return(BadRequest(invalidOperation.Message)); } catch (Exception execption) { var result = new StatusCodeResult((int)HttpStatusCode.InternalServerError); return(result); } }