public HttpResponseMessage GetStudentTim() { List <StudentTim> result = new List <StudentTim>(); using (var connection = new SqlConnection(ConnectionString)) { SqlCommand sqlCmd = new SqlCommand("select * from Tim.StudentTim", connection) { CommandType = CommandType.Text }; try { connection.Open(); using (SqlDataReader dataReader = sqlCmd.ExecuteReader()) { while (dataReader.Read()) { var studentTim = new StudentTim { StudentTimID = Convert.ToInt32(dataReader["StudentTimID"]), TimID = Convert.ToInt32(dataReader["TimID"]), StudentID = Convert.ToInt32(dataReader["StudentID"]) }; result.Add(studentTim); } } } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error retrieving data")); } } return(Request.CreateResponse(HttpStatusCode.OK, result)); }
public HttpResponseMessage CreateStudentTim([FromBody] StudentTim studentTim) { using (var connection = new SqlConnection(ConnectionString)) { SqlCommand sqlCmd = new SqlCommand("INSERT INTO Tim.StudentTim VALUES(@TimID, @StudentID)", connection); sqlCmd.Parameters.AddWithValue("TimID", studentTim.TimID); sqlCmd.Parameters.AddWithValue("StudentID", studentTim.StudentID); try { connection.Open(); int i = sqlCmd.ExecuteNonQuery(); SqlCommand sqlStudentTim = new SqlCommand("SELECT TOP 1 * FROM Tim.StudentTim ORDER BY StudentTimID DESC", connection); StudentTim last = new StudentTim(); using (SqlDataReader studentTimRead = sqlStudentTim.ExecuteReader()) { while (studentTimRead.Read()) { last.StudentTimID = Convert.ToInt32(studentTimRead["StudentTimID"]); last.TimID = Convert.ToInt32(studentTimRead["TimID"]); last.StudentID = Convert.ToInt32(studentTimRead["StudentID"]); } } var response = Request.CreateResponse(HttpStatusCode.Created, last); response.Headers.Location = new Uri(Request.RequestUri + "/" + last.StudentTimID); return(response); } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error 404 Bad request")); } } }
public ActionResult DodajStudentaUTim([Bind(Include = "Id,StudentId,TimId")] StudentTim studentTim) { Tim tim = db.Tims.FirstOrDefault(t => t.Id == studentTim.TimId); Kolegij kolegij = db.Kolegij.FirstOrDefault(k => k.Id == tim.KolegijId); if (ModelState.IsValid) { int brojStudenataUTimu = db.StudentTims.Where(st => st.TimId == tim.Id).Count(); if (brojStudenataUTimu + 1 <= kolegij.MaxVelicinaGrupe) { StudentTim newStudentTime = db.StudentTims.Add(studentTim); db.SaveChanges(); } } return(RedirectToAction("UrediTim", new { kolegijId = tim.KolegijId })); }
public ActionResult Create([Bind(Include = "Id,Naziv,KolegijId")] Tim tim) { if (ModelState.IsValid) { string id = HttpContext.User.Identity.GetUserId(); Student student = db.Student.Where(s => s.UserId == id).FirstOrDefault(); Tim newTim = db.Tims.Add(tim); db.SaveChanges(); StudentTim newStudentTim = new StudentTim { StudentId = student.Id, TimId = newTim.Id }; db.StudentTims.Add(newStudentTim); db.SaveChanges(); } return(RedirectToAction("UrediTim", new { kolegijId = tim.KolegijId })); }
public HttpResponseMessage UpdateStudentTim([FromBody] StudentTim studentTim) { using (var connection = new SqlConnection(ConnectionString)) { try { using (var sqlCmd = new SqlCommand("UPDATE Tim.StudentTim SET StudentID=@StudentID, TimID=@TimID WHERE StudentTimID=@id")) { connection.Open(); sqlCmd.Connection = connection; sqlCmd.Parameters.AddWithValue("StudentID", studentTim.StudentID); sqlCmd.Parameters.AddWithValue("TimID", studentTim.TimID); sqlCmd.Parameters.AddWithValue("id", studentTim.StudentTimID); sqlCmd.ExecuteNonQuery(); } } catch (Exception) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error 400 Bad Request")); } } return(Request.CreateResponse(HttpStatusCode.OK, "Updated Successfully")); }
public ActionResult MakniStudentaIzTima(int studentId, int timId, int kolegijId) { StudentTim studentTim = db.StudentTims.FirstOrDefault(st => st.StudentId == studentId && st.TimId == timId); db.StudentTims.Remove(studentTim); db.SaveChanges(); int ostaloStudenata = db.StudentTims.Select(st => st.TimId == timId).Count(); if (ostaloStudenata == 0) { Tim timZaBrisat = db.Tims.FirstOrDefault(t => t.Id == timId); db.Tims.Remove(timZaBrisat); db.SaveChanges(); TimSeminarDatumSeminar timSeminarDatumSeminar = db.TimSeminarDatumSeminars.FirstOrDefault(tsds => tsds.TimId == timId); db.TimSeminarDatumSeminars.Remove(timSeminarDatumSeminar); db.SaveChanges(); } return(RedirectToAction("UrediTim", new { kolegijId = kolegijId })); }