public IActionResult InsertPrescription(InsertPrescriptionRequest request) { var result = _service.InsertPrescription(request); if (result == null) { return(BadRequest()); } else { return(Ok(result)); } }
public InsertPrescriptionResponse InsertPrescription(InsertPrescriptionRequest request) { InsertPrescriptionResponse response; // tutaj if ze sprawdzeniem czy duedate jest starsza od date i ewentualne zwrocenie nulla using (var con = new SqlConnection(ConString)) using (var com = new SqlCommand()) { com.Connection = con; con.Open(); var tran = con.BeginTransaction(); com.Transaction = tran; try { com.CommandText = "select count(*)+1 as Id from Prescription"; int Id; using (var dr = com.ExecuteReader()) { if (!dr.HasRows) { return(null); } Id = int.Parse(dr["Id"].ToString()); } com.Parameters.Clear(); com.CommandText = "select * from Patient where IdPatient = @patient"; com.Parameters.AddWithValue("patient", request.IdPatient); using (var dr = com.ExecuteReader()) { if (!dr.HasRows) { return(null); } } com.CommandText = "select * from Doctor where IdDoctor = @doctor"; com.Parameters.AddWithValue("doctor", request.IdPatient); using (var dr = com.ExecuteReader()) { if (!dr.HasRows) { return(null); } } com.CommandText = "insert into prescription (IdPrescription,Date, DueDate,IdPatient,IdDoctor)" + "values(@IdPrescription,@Date,@DueDate,@IdPatient,@IdDoctor)"; com.Parameters.AddWithValue("IdPrescription", Id); com.Parameters.AddWithValue("Date", request.Date); com.Parameters.AddWithValue("DueDate", request.DueDate); com.Parameters.AddWithValue("IdPatient", request.IdPatient); com.Parameters.AddWithValue("IdDoctor", request.IdDoctor); com.ExecuteNonQuery(); tran.Commit(); response = new InsertPrescriptionResponse(); response.IdPrescription = Id; response.Date = request.Date; response.DueDate = request.DueDate; response.IdPatient = request.IdPatient; response.IdDoctor = request.IdPatient; return(response); } catch (SqlException exc) { tran.Rollback(); return(null); } } }