Exemple #1
0
        public IActionResult DeleteVehicle(int vehicleID)
        {
            try
            {
                SqlConnection conn = new SqlConnection(Utils.connectionString);
                SqlCommand    cmd  = new SqlCommand();

                cmd.Connection = conn;

                cmd.CommandText = " delete from Vehicles " +
                                  " where VehicleID = @vehicleID";

                cmd.Parameters.AddWithValue("@vehicleID", vehicleID); // vehicleID for URI

                conn.Open();

                DataTable dt = new DataTable();

                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    dt.Load(dr);

                    VehicleForCreation add_vehicle = new VehicleForCreation();

                    return(Ok("Vehicle [" + vehicleID.ToString() + "] Deleted successfully!"));
                }
            }
            catch (SqlException sqlEx)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, sqlEx));
            }
        }
Exemple #2
0
        public IActionResult UpdateVehicle(int vehicleID, [FromBody] VehicleForCreation updateVehicleItem)
        {
            try
            {
                SqlConnection conn = new SqlConnection(Utils.connectionString);
                SqlCommand    cmd  = new SqlCommand();

                cmd.Connection = conn;
                //cmd.CommandText = "select * from Vehicles where VehicleID = " + vehicleIDParam;
                cmd.CommandText = " update Vehicles " +
                                  " set DriveTypeID = @DriveTypeID,EngineDescriptionID = @EngineDescription,MakeID = @MakeID,ModelID = @ModelID,ConstructionYearID = @ConstructionYearID , " +
                                  " ModifyDate = GETUTCDATE(),VehiclePrice = @VehiclePrice " +
                                  " where VehicleID = @vehicleID";

                cmd.Parameters.AddWithValue("@vehicleID", vehicleID);  // vehicleID for URI

                cmd.Parameters.AddWithValue("@DriveTypeID", updateVehicleItem.DriveTypeID);
                cmd.Parameters.AddWithValue("@EngineDescription", updateVehicleItem.EngineDescriptionID);
                cmd.Parameters.AddWithValue("@MakeID", updateVehicleItem.MakeID);
                cmd.Parameters.AddWithValue("@ModelID", updateVehicleItem.ModelID);
                cmd.Parameters.AddWithValue("@ConstructionYearID", updateVehicleItem.ConstructionYearID);
                cmd.Parameters.AddWithValue("@ModifyDate", updateVehicleItem.ModifyDate = DateTime.Now);
                cmd.Parameters.AddWithValue("@VehiclePrice", updateVehicleItem.VehiclePrice);


                conn.Open();

                DataTable dt = new DataTable();

                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    dt.Load(dr);

                    VehicleForCreation add_vehicle = new VehicleForCreation();



                    return(Ok("Vehicle [" + vehicleID + "] Updated successfully!"));
                }
            }
            catch (SqlException sqlEx)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, sqlEx));
            }
        }
        public IActionResult AddVehicle([FromBody] VehicleForCreation vItem)
        {
            try
            {
                SqlConnection conn = new SqlConnection(Utils.connectionString);
                SqlCommand    cmd  = new SqlCommand();



                cmd.Connection = conn; //to open the connection
                //cmd.CommandText = "select * from Vehicles where VehicleID = " + vehicleIDParam;
                cmd.CommandText = "INSERT INTO Vehicles (DriveTypeID,EngineDescriptionID,MakeID,ModelID,ConstructionYearID,ModifyDate,VehiclePrice)" +
                                  "Values(@DriveTypeID,@EngineDescription,@MakeID,@ModelID,@ConstructionYearID,@ModifyDate,@VehiclePrice)";


                cmd.Parameters.AddWithValue("@DriveTypeID", SqlDbType.Int).Value = vItem.DriveTypeID;
                cmd.Parameters.AddWithValue("@EngineDescription", vItem.EngineDescriptionID);
                cmd.Parameters.AddWithValue("@MakeID", vItem.MakeID);
                cmd.Parameters.AddWithValue("@ModelID", vItem.ModelID);
                cmd.Parameters.AddWithValue("@ConstructionYearID", vItem.ConstructionYearID);
                cmd.Parameters.AddWithValue("@ModifyDate", vItem.ModifyDate = DateTime.Now);
                cmd.Parameters.AddWithValue("@VehiclePrice", vItem.VehiclePrice);



                conn.Open();

                DataTable dt = new DataTable();

                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    dt.Load(dr);


                    return(Ok("Vehicle Added successfully!"));
                }
            }
            catch (SqlException sqlEx)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, sqlEx));
            }
        }
Exemple #4
0
        public async Task <IActionResult> Update([FromBody] VehicleForCreation vehicleForUpdateDto, int id)
        {
            var vehicle = await _repo.Get(id);

            if (vehicle == null)
            {
                return(NotFound());
            }

            _mapper.Map(vehicleForUpdateDto, vehicle);
            vehicle.LastUpdate = DateTime.UtcNow;

            if (await _unitOfWork.CompleteAsync())
            {
                vehicle = await _repo.Get(id);

                var vehicleToReturn = _mapper.Map <VehicleForDetailed>(vehicle);
                return(Ok(vehicleToReturn));
            }

            return(BadRequest("An error occured while vehicle updating."));
        }
Exemple #5
0
        public async Task <IActionResult> Create([FromBody] VehicleForCreation vehicleForCreationDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var vehicle = _mapper.Map <Vehicle>(vehicleForCreationDto);

            vehicle.LastUpdate = DateTime.UtcNow;
            _repo.Add(vehicle);

            if (await _unitOfWork.CompleteAsync())
            {
                vehicle = await _repo.Get(vehicle.Id);

                var vehicleToReturn = _mapper.Map <VehicleForDetailed>(vehicle);
                return(Ok(vehicleToReturn));
            }

            return(BadRequest("An error occured while vehicle creating."));
        }