public IActionResult GetMedicament(int id)
 {
     try
     {
         GetMedicamentResponse res = MedicamentsService.getMedicament(id);
         return(Ok(res));
     } catch (Exception ex)
     {
         return(NotFound(ex.Message));
     }
 }
 public IActionResult GetMedication(int id)
 {
     try
     {
         GetMedicamentResponse response = _service.GetMedication(id);
         return(Ok(response));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
        public GetMedicamentResponse GetMedication(int id)
        {
            GetMedicamentResponse medicamentResponse = new GetMedicamentResponse();

            using (var con = new SqlConnection(ConnString))
                using (var com = new SqlCommand())
                {
                    com.Connection = con;
                    con.Open();

                    try
                    {
                        com.CommandText = "select * from Medicament where IdMedicament=@idMedicament";
                        com.Parameters.AddWithValue("idMedicament", id);

                        var dr = com.ExecuteReader();
                        if (!dr.HasRows)
                        {
                            throw new Exception("Nie ma takiego leku");
                        }
                        if (dr.Read())
                        {
                            medicamentResponse.IdMedicament   = (int)dr["IdMedicament"];
                            medicamentResponse.Name           = dr["Name"].ToString();
                            medicamentResponse.Description    = dr["Description"].ToString();
                            medicamentResponse.MedicamentType = dr["Type"].ToString();
                            medicamentResponse.Prescriptions  = new List <Prescription>();
                        }
                        else
                        {
                            //  dr.Close();
                            throw new Exception("Nie ma takiego leku");
                        }
                        dr.Close();

                        List <Prescription> prescriptionsList = new List <Prescription>();

                        com.CommandText =
                            "select Prescription.IdPrescription,Date,DueDate,IdPatient,IdDoctor,Dose,Details from Prescription join Prescription_Medicament on Prescription.IdPrescription=Prescription_Medicament.IdPrescription where IdMedicament=@MedId order by Date desc";
                        com.Parameters.AddWithValue("MedId", id);

                        dr = com.ExecuteReader();

                        while (dr.Read())
                        {
                            prescriptionsList.Add(
                                new Prescription()
                            {
                                IdPrescription = (int)dr["IdPrescription"],
                                Date           = DateTime.Parse(dr["Date"].ToString()),
                                DueDate        = DateTime.Parse(dr["DueDate"].ToString()),
                                IdPatient      = (int)dr["IdPatient"],
                                IdDoctor       = (int)dr["IdDoctor"],
                                Dose           = (int)dr["Dose"],
                                Details        = dr["Details"].ToString()
                            });
                        }
                        dr.Close();

                        medicamentResponse.Prescriptions = prescriptionsList;
                    }
                    catch (SqlException e)
                    {
                        throw new Exception(e.Message);
                    }
                }

            return(medicamentResponse);
        }
示例#4
0
        public GetMedicamentResponse GetMedicament(int id)
        {
            using (var connection = new SqlConnection())
                using (var command = new SqlCommand())
                {
                    connection.ConnectionString = ConString;
                    command.Connection          = connection;

                    connection.Open();
                    var transaction = connection.BeginTransaction();
                    try
                    {
                        command.Transaction = transaction;

                        command.CommandText =
                            $"Select Name, Description, Type from Medicament where IdMedicament = {id}";

                        var dataReader = command.ExecuteReader();

                        if (!dataReader.Read())
                        {
                            dataReader.Close();
                            transaction.Rollback();
                            return(null);
                        }

                        var medicamentDetails = new GetMedicamentResponse()
                        {
                            Name             = (dataReader["Name"].ToString()),
                            Description      = (dataReader["Description"].ToString()),
                            Type             = (dataReader["Type"].ToString()),
                            PrescriptionList = new List <Prescription>()
                        };

                        command.CommandText =
                            $"Select IdPrescription FROM Prescription_Medicament where IdMedicament = {id}";
                        dataReader.Close();

                        dataReader = command.ExecuteReader();
                        var prescriptionsIdList = new List <int>();

                        while (dataReader.Read())
                        {
                            prescriptionsIdList.Add(int.Parse(dataReader["IdPrescription"].ToString()));
                        }

                        foreach (var idPres in prescriptionsIdList)
                        {
                            dataReader.Close();
                            command.CommandText =
                                $"SELECT Dose, Details FROM Prescription_Medicament WHERE IdMedicament = {id} AND IdPrescription = {idPres}";
                            dataReader = command.ExecuteReader();

                            var dose    = int.Parse(dataReader["Dose"].ToString());
                            var details = dataReader["Details"].ToString();
                            dataReader.Close();

                            command.CommandText =
                                $"SELECT Date, DueDate, IdPatient, IdDoctor FROM Prescription WHERE IdPrescription = {idPres} order by Date desc";
                            dataReader = command.ExecuteReader();
                            medicamentDetails.PrescriptionList.Add(new Prescription()
                            {
                                IdPrescription = idPres,
                                Date           = DateTime.Parse((string)dataReader["Date".ToString()]),
                                DueDate        = DateTime.Parse((string)dataReader["DueDate"].ToString()),
                                IdPatient      = int.Parse(dataReader["IdPatient"].ToString()),
                                IdDoctor       = int.Parse(dataReader["IdDoctor"].ToString())
                            });
                        }

                        dataReader.Close();
                        transaction.Commit();
                        return(medicamentDetails);
                    }
                    catch (SqlException e)
                    {
                        Console.WriteLine(e.ToString());
                        transaction.Rollback();
                        return(null);
                    }
                }
        }
        public GetMedicamentResponse getMedicament(int id)
        {
            List <Prescription> prescList = new List <Prescription>();

            using (var con = new SqlConnection(myConnection))
                using (var com = new SqlCommand())
                {
                    com.Connection = con;

                    com.Parameters.AddWithValue("IdMedicament", id);
                    com.CommandText = "SELECT * FROM Medicament WHERE IdMedicament =@IdMedicament;";
                    con.Open();

                    var dr = com.ExecuteReader();


                    Medicament medicament;
                    if (dr.Read())
                    {
                        medicament = new Medicament
                        {
                            IdMed       = id,
                            Name        = dr["Name"].ToString(),
                            Description = dr["Description"].ToString(),
                            Type        = dr["Type"].ToString()
                        };
                    }
                    else
                    {
                        throw new Exception("Nie znaleziono laku o takim id!");
                    }


                    com.CommandText = " SELECT Prescription_Medicament.Dose as dose, Prescription_Medicament.Details as details, Prescription.IdPrescription as idPresc, Prescription.Date as date, DueDate as dueDate, IdPatient as idPatient, IdDoctor as idDoctor FROM Prescription JOIN Prescription_Medicament ON Prescription.IdPrescription = Prescription_Medicament.IdPrescription WHERE IdMedicament = @IdMedicament;";

                    dr.Close();
                    dr = com.ExecuteReader();

                    while (dr.Read())
                    {
                        Prescription p = new Prescription
                        {
                            IdPresc   = (int)dr["idPresc"],
                            Date      = dr["date"].ToString(),
                            DueDate   = dr["dueDate"].ToString(),
                            IdPatient = (int)dr["idPatient"],
                            IdDoctor  = (int)dr["idDoctor"],
                            Dose      = (int)dr["dose"],
                            Details   = dr["dose"].ToString()
                        };

                        prescList.Add(p);
                    }

                    //var tran = con.BeginTransaction("SampleTransaction");
                    //com.Transaction = tran;

                    //var dr = com.ExecuteReader();

                    GetMedicamentResponse resp = new GetMedicamentResponse
                    {
                        Medicament    = medicament,
                        Prescriptions = prescList
                    };


                    return(resp);
                }
        }