Created
January 23, 2013 15:44
-
-
Save eldewall/4608364 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/cars | |
| [HttpPost] | |
| public JsonResult Create(Car 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.SalesObjectCar so = new data.SalesObjectCar(CurrentIdentity, db); | |
| so.FacilityId = request.facility_id.Value; | |
| so.SalesObjectType = data.SalesObjectType.Car; | |
| 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.Value; | |
| 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.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; | |
| // currently unused: | |
| so.Warranty = request.warranty ?? String.Empty; | |
| so.ChassisNumber = request.chassis_number ?? String.Empty; | |
| so.MaxVelocity = request.max_velocity; | |
| so.Co2EmissionFT1 = request.co2_emission_ft1; | |
| so.FuelConsumpFT1 = request.fuel_consump_ft1; | |
| Transaction transaction = db.BeginTransaction("CarAdd"); | |
| 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