Exemplo n.º 1
0
        public IActionResult AddAnimal(AnimlaRequest request)
        {
            using (var sqlConnection = new SqlConnection(@"Data Source=db-mssql;Initial Catalog=s19515;Integrated Security=True"))
            {
                using (var com = new SqlCommand())
                {
                    com.Connection = sqlConnection;

                    sqlConnection.Open();
                    var tran = sqlConnection.BeginTransaction();
                    com.Transaction = tran;

                    try
                    {
                        //Check if all required data has been delivered.
                        if (string.IsNullOrWhiteSpace(request.Name) || string.IsNullOrWhiteSpace(request.Type) || string.IsNullOrWhiteSpace(request.IdOwner.ToString()))
                        {
                            tran.Rollback();
                            return(StatusCode(401));
                        }



                        //create the animal
                        string   aN   = request.Name;
                        string   aT   = request.Type;
                        DateTime aAD  = request.AdmissionDate;
                        int      aOID = request.IdOwner;


                        //add the animal
                        com.CommandText = "INSERT INTO Animal VALUES(@name, @TYPE, @Aday, @Owner);";
                        com.Parameters.AddWithValue("name", aN);
                        com.Parameters.AddWithValue("TYPE", aT);
                        com.Parameters.AddWithValue("Aday", aAD);
                        com.Parameters.AddWithValue("Owner", aOID);

                        com.ExecuteNonQuery();

                        //if the list of procedures was given, add it to database
                        if (!string.IsNullOrWhiteSpace(request.procedures.ToString()))
                        {
                            foreach (Procedure p in request.procedures)
                            {
                                com.CommandText = "INSERT INTO Procedure_Animal VALUES(@ProcId, (select IdAnimal from Animal where Name=@anName), @procD)";
                                com.Parameters.AddWithValue("ProcId", p.IdProcedure);
                                com.Parameters.AddWithValue("anName", request.Name);
                                com.Parameters.AddWithValue("procD", p.procedureDate);

                                com.ExecuteNonQuery();
                            }
                        }

                        tran.Commit();

                        return(Ok(200));
                    }
                    catch (SqlException exc)
                    {
                        tran.Rollback();
                        return(StatusCode(401));
                    }
                }
            }
        }
Exemplo n.º 2
0
 public IActionResult addAnimal(AnimlaRequest request)
 {
     return(_service.AddAnimal(request));
 }