public IActionResult FireTruckToAction(FireTrucksRequest request) { var response = _service.FireTruckToAction(request); if (response != null) { return(Ok(response)); } return(BadRequest()); }
public FireTruckResponse FireTruckToAction(FireTrucksRequest request) { var response = new FireTruckResponse(); using (var connection = new SqlConnection("Data Source=db-mssql;Initial Catalog=s9405;Integrated Security=true")) using (var command = new SqlCommand()) { command.Connection = connection; connection.Open(); var transaction = connection.BeginTransaction(); command.Transaction = transaction; var date = DateTime.Now; command.Parameters.AddWithValue("IdFireTruck", request.idFireTruck); command.Parameters.AddWithValue("IdAction", request.idAction); command.Parameters.AddWithValue("AssigmentDate", date); command.CommandText = "select max(IdFireTruckAction) from FireTruck_Action"; var dr = command.ExecuteReader(); int IdFireTruckAction = dr.GetInt32(0); command.Parameters.AddWithValue("IdFireTruckAction", IdFireTruckAction); dr.Close(); command.CommandText = "Select * from FireTruck where IdFireTruck = @IdFireTruck"; dr = command.ExecuteReader(); if (!dr.HasRows) { transaction.Rollback(); throw new ArgumentException("Brak podanego wozu"); } dr.Close(); command.CommandText = "Select * from Actions where IdActions = @IdActions"; dr = command.ExecuteReader(); if (!dr.HasRows) { transaction.Rollback(); throw new ArgumentException("Brak podanej akcji"); } dr.Close(); command.CommandText = "INSERT INTO FireTruck_Action(IdFireTruckAction ,IdFireTruck,IdAction, AssigmentDate)" + " values (@IdFireTruckAction,@IdFireTruck,@IdAction, @AssigmentDate)"; dr = command.ExecuteReader(); dr.Close(); transaction.Commit(); command.Parameters.Clear(); connection.Close(); response.IdAction = request.idAction; response.IdFireTruck = request.idFireTruck; response.AssignmentDate = date; response.IdFireTruckAction = IdFireTruckAction; } return(response); }