Last active
December 11, 2015 13:38
-
-
Save eldewall/4608325 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // POST: /api/transports | |
| [HttpPost] | |
| public JsonResult Create(Transport request) { | |
| if (!CurrentIdentity.Can(data.SpecialRights.ChangePrice)) { | |
| return Json(ApiResult(Result.RightsError("Du har inte rättighet att skapa objekt.")), JsonRequestBehavior.DenyGet); | |
| } | |
| if (!ModelState.IsValid) { | |
| Response.StatusCode = 422; | |
| return Json(ApiResult(ModelErrors()), JsonRequestBehavior.AllowGet); | |
| } | |
| using (ConnectionProvider db = new ConnectionProvider()) { | |
| data.SalesObjectTransport so = new data.SalesObjectTransport(CurrentIdentity, db); | |
| so.FacilityId = request.facility_id.Value; | |
| so.SalesObjectType = data.SalesObjectType.Transport; | |
| so.IsUsed = !request.is_new; | |
| so.Visibility = Visibility.GetVisibility(false, request.display_bytbil, request.display_access, request.display_shopdisplay, true, true, true); | |
| so.Price = PriceFormater.FromString(request.price, false, true); | |
| so.CurrentPrice = PriceFormater.FromString(request.current_price, true, request.use_current_price); | |
| so.IsVatDeductable = request.is_vat_deductable; | |
| so.Information = request.information; | |
| so.SalesPitch = request.sales_pitch; | |
| so.Identification = request.identification; | |
| so.BrandId = request.brand_id; | |
| so.ModelId = request.model_id; | |
| so.VersionId = request.version_id; | |
| so.VehicleYear = request.vehicle_year.Value; | |
| so.RegistrationDate = new DateTime(request.registration_year.Value, request.registration_month.Value, 1); | |
| so.Kilometers = Uom.DistanceKm(request.km, request.km_uom_id).Value; | |
| so.KilometersUom = request.km_uom_id; | |
| so.BodyId = request.body_id; | |
| so.GearboxId = request.gearbox_id; | |
| so.DriveWheelsId = request.drivewheels_id; | |
| so.Fueltype1Id = request.fueltype1_id; | |
| so.Fueltype2Id = request.fueltype2_id; | |
| so.EngineCC = Uom.VolumeCC(request.engine_cc, request.engine_cc_uom_id).Value; | |
| so.EngineCCUom = request.engine_cc_uom_id; | |
| so.PowerKw = Uom.PowerKw(request.power, request.power_uom_id).Value; | |
| so.PowerKwUom = request.power_uom_id; | |
| so.TotalWeight = Uom.WeightKg(request.total_weight, request.total_weight_uom_id); | |
| so.TotalWeightUom = request.total_weight_uom_id; | |
| so.PassengerNumber = request.passenger_number_id; | |
| so.RegisteredColor = request.registered_color; | |
| so.Color = request.color; | |
| so.Equipment = Equipment.GetMask(request.equipment); | |
| so.EquipmentText = request.equipment_text; | |
| // temporarily placed: | |
| //so.IsEnvironment = request.is_environment; | |
| //so.CargoLength = Uom.LengthMm(request.cargo_length, request.cargo_length_uom_id); | |
| //so.CargoLengthUom = request.cargo_length_uom_id; | |
| //so.CargoWidth = Uom.LengthMm(request.cargo_width, request.cargo_width_uom_id); | |
| //so.CargoWidthUom = request.cargo_width_uom_id; | |
| //so.CargoHeight = Uom.LengthMm(request.cargo_height, request.cargo_height_uom_id); | |
| //so.CargoHeightUom = request.cargo_height_uom_id; | |
| //so.Torsion = Uom.TorsionNm(request.torsion, Uom.UomId.Nm); | |
| // currently unused: | |
| so.ChassisNumber = request.chassis_number ?? String.Empty; | |
| so.Warranty = request.warranty ?? String.Empty; | |
| so.MaxVelocity = Uom.SpeedKm_h(request.max_velocity, Uom.UomId.Km_h); | |
| so.Co2EmissionFT1 = request.co2_emission_ft1; | |
| so.FuelConsumpFT1 = Uom.ConsumptionL100km(request.fuel_consump_ft1, Uom.UomId.L_100km); | |
| Transaction transaction = db.BeginTransaction("TransportAdd"); | |
| Result addResult = so.Add(CurrentIdentity, db); | |
| if (!addResult.Ok) { | |
| transaction.Rollback(); | |
| return Json(ApiResult(addResult), JsonRequestBehavior.DenyGet); | |
| } | |
| int objectId = addResult.Out<int>("SalesObjectId"); | |
| Result sellerResult; | |
| if (request.seller_id.HasValue) { | |
| sellerResult = data.SalesObjectAccount.SetForSalesObject(CurrentIdentity, db, objectId, request.seller_id.Value); | |
| } else { | |
| sellerResult = data.SalesObjectAccount.RemoveForSalesObject(CurrentIdentity, db, objectId); | |
| } | |
| if (!sellerResult.Ok) { | |
| transaction.Rollback(); | |
| return Json(ApiResult(sellerResult), JsonRequestBehavior.DenyGet); | |
| } | |
| transaction.Commit(); | |
| return Json(ApiResult(addResult, objectId)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment