public IActionResult Add(PrescriptionRequestDto prescription) { if (_prescriptionService.AddPrescription(prescription)) { return(Ok("Dodano!")); } return(BadRequest()); }
public bool AddPrescription(PrescriptionRequestDto prescription) { using (var connection = new SqlConnection(conString)) using (var command = new SqlCommand()) { connection.Open(); var transaction = connection.BeginTransaction(); command.Transaction = transaction; command.Connection = connection; command.CommandText = "insert into Prescription(Date, DueDate, IdDoctor,IdPationt) values (@date,@dueDate @doctor,@patient); SELECT SCOPE_IDENTITY()"; command.CommandType = CommandType.Text; command.Parameters.AddWithValue("date", prescription.Date); command.Parameters.AddWithValue("dueDate", prescription.DueDate); command.Parameters.AddWithValue("doctor", prescription.IdDoctor); command.Parameters.AddWithValue("patient", prescription.IdPationt); int id = Convert.ToInt32(command.ExecuteScalar()); Console.WriteLine(id); if (prescription.Medicaments == null) { return(true); } foreach (var medicament in prescription.Medicaments) { command.CommandText = $"insert into Prescription_Medicament(idMedicament, idPrescription, dose, details) values({medicament.Id}, {medicament.Name}, {medicament.Description},{medicament.Type})"; command.Parameters["name"].Value = medicament.Name; command.Parameters["description"].Value = medicament.Description; command.Parameters["type"].Value = medicament.Type; command.ExecuteNonQuery(); } transaction.Commit(); } return(true); }