public HttpResponseMessage UpdateStudentPorukaPrimalac([FromBody] StudentPorukaPrimalac studentPorukaPrimalac) { using (var connection = new SqlConnection(ConnectionString)) { try { using (var sqlCmd = new SqlCommand("UPDATE Poruka.StudentPorukaPrimalac SET PorukaID=@PorukaID, StudentID=@StudentID WHERE StudentPorukaPrimalacID=@id")) { connection.Open(); sqlCmd.Connection = connection; sqlCmd.Parameters.AddWithValue("PorukaID", studentPorukaPrimalac.PorukaID); sqlCmd.Parameters.AddWithValue("StudentID", studentPorukaPrimalac.StudentID); sqlCmd.Parameters.AddWithValue("id", studentPorukaPrimalac.StudentPorukaPrimalacID); int rowAffected = sqlCmd.ExecuteNonQuery(); if (rowAffected == 0) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error 400 BadRequest")); } } } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error 400 Bad Request")); } } return(Request.CreateResponse(HttpStatusCode.OK, "Updated Successfully")); }
public HttpResponseMessage GetStudentPorukaPrimalac() { List <StudentPorukaPrimalac> result = new List <StudentPorukaPrimalac>(); using (var connection = new SqlConnection(ConnectionString)) { SqlCommand sqlCmd = new SqlCommand("select * from Poruka.StudentPorukaPrimalac", connection) { CommandType = CommandType.Text }; try { connection.Open(); using (SqlDataReader dataReader = sqlCmd.ExecuteReader()) { while (dataReader.Read()) { var studentPorukaPrimalac = new StudentPorukaPrimalac { StudentPorukaPrimalacID = Convert.ToInt32(dataReader["StudentPorukaPrimalacID"]), PorukaID = Convert.ToInt32(dataReader["PorukaID"]), StudentID = Convert.ToInt32(dataReader["StudentID"]) }; result.Add(studentPorukaPrimalac); } } } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error retrieving data")); } } return(Request.CreateResponse(HttpStatusCode.OK, result)); }
public HttpResponseMessage CreateStudentPorukaPrimalac([FromBody] StudentPorukaPrimalac studentPorukaPrimalac) { using (var connection = new SqlConnection(ConnectionString)) { SqlCommand sqlCmd = new SqlCommand("INSERT INTO Poruka.StudentPorukaPrimalac VALUES(@PorukaID, @StudentID)", connection); sqlCmd.Parameters.AddWithValue("PorukaID", studentPorukaPrimalac.PorukaID); sqlCmd.Parameters.AddWithValue("StudentID", studentPorukaPrimalac.StudentID); try { connection.Open(); int i = sqlCmd.ExecuteNonQuery(); SqlCommand sqlstudentPorukaPrimalac = new SqlCommand("SELECT TOP 1 * FROM Poruka.StudentPorukaPrimalac ORDER BY StudentPorukaPrimalacID DESC", connection); StudentPorukaPrimalac last = new StudentPorukaPrimalac(); using (SqlDataReader studentPorukaPrimalacRead = sqlstudentPorukaPrimalac.ExecuteReader()) { while (studentPorukaPrimalacRead.Read()) { last.StudentPorukaPrimalacID = Convert.ToInt32(studentPorukaPrimalacRead["StudentPorukaPrimalacID"]); last.PorukaID = Convert.ToInt32(studentPorukaPrimalacRead["PorukaID"]); last.StudentID = Convert.ToInt32(studentPorukaPrimalacRead["StudentID"]); } } var response = Request.CreateResponse(HttpStatusCode.Created, last); response.Headers.Location = new Uri(Request.RequestUri + "/" + last.StudentPorukaPrimalacID); return(response); } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error 404 Bad request")); } } }