public IActionResult post(AddStudentRequest request) { //EF context.Database.BeginTransaction(); var studies = context.studies.FirstOrDefault(a => a.Name == request.Studies); if (studies == null) { context.Database.RollbackTransaction(); return(NotFound("Nie ma takiego kierunku")); } var enroll = context.enrollments.FirstOrDefault(a => a.IdStudy == studies.IdStudy); //"select coalesce(min(t1.IdEnrollment) + 1, 0) from EnrollmentN t1 left outer join EnrollmentN t2 on t1.IdEnrollment = t2.IdEnrollment - 1 where t2.IdEnrollment is null"; //view findMinStudent select * from findMinStudent; //Brak pomyslu jak to zrobic za pomoc EF int newid = context.Database.ExecuteSqlRaw("select * from findMinStudent"); Student s = new Student { IdStudent = newid, FirstName = request.FirstName, LastName = request.LastName, IndexNumber = request.IndexNumber, BirthDate = request.BirthDate, IdEnrollment = enroll.IdEnrollment }; context.students.Add(s); context.Database.CommitTransaction(); context.SaveChanges(); //Stare //To przerobić na SameExpection i try catch, to jest fuuuuu //Przychodzi ci student i robi takie cos :C dbService.AddStudnet(request); if (dbService.getMsg() == -1) { return(BadRequest("Can not add")); } if (dbService.getMsg() == -2) { return(BadRequest("Studies not found")); } if (dbService.getMsg() == -3) { return(BadRequest("Stident index is not uniqe")); } if (dbService.getMsg() == -4) { return(BadRequest("Something gone wrong :C")); } return(Created("", dbService.GetEnrollment())); }
public IActionResult post(AddStudentRequest request) { dbService.AddStudnet(request); if (dbService.getMsg() == -1) { return(BadRequest("Can not add")); } if (dbService.getMsg() == -2) { return(BadRequest("Studies not found")); } if (dbService.getMsg() == -3) { return(BadRequest("Stident index is not uniqe")); } if (dbService.getMsg() == -4) { return(BadRequest("Something gone wrong :C")); } return(Created("", dbService.GetEnrollment())); }