public object Save(Entities.Listing Listing) { if (!ModelState.IsValid) { return(Error(ModelState)); } if (Listing.Save()) { return(Success(new { Entity = Listing.Json() })); } return(Error()); }
public object Add(ListingModel Model) { if (!ModelState.IsValid) { return(Error(ModelState)); } var listing = new Entities.Listing { AvailableWithoutFuel = Model.AvailableWithoutFuel, AvailableWithFuel = Model.AvailableWithFuel, Brand = Model.Brand, CategoryId = Model.CategoryId, ConditionId = Model.ConditionId, Latitude = Model.Latitude, Longitude = Model.Longitude, Description = Model.Description, GroupServices = Model.GroupServices, HorsePower = Model.HorsePower, Location = Model.Location, StatusId = Entities.ListingStatus.Live, Title = Model.Title, UserId = CurrentUser.Id, Year = Model.Year }; listing.Services = new List <Entities.Service>(); foreach (var service in Model.Services) { listing.Services.Add(new Entities.Service { DistanceUnitId = service.DistanceUnitId, FuelPrice = service.FuelPrice, FuelPerQuantityUnit = service.FuelPerQuantityUnit, MaximumDistance = service.MaximumDistance, MinimumQuantity = service.MinimumQuantity, Mobile = service.Mobile || listing.CategoryId == Entities.Category.TractorsId || listing.CategoryId == Entities.Category.LorriesId, PricePerDistanceUnit = service.Mobile ? service.PricePerDistanceUnit : 0, PricePerQuantityUnit = service.PricePerQuantityUnit, QuantityUnitId = service.QuantityUnitId, CategoryId = service.CategoryId, TimePerQuantityUnit = service.TimePerQuantityUnit, TimeUnitId = service.TimeUnitId, TotalVolume = service.TotalVolume }); } if (listing.Services.Count == 0) { return(Error("You must enable at least on service")); } if (listing.CategoryId == Entities.Category.LorriesId && listing.Services.First().TotalVolume == 0) { return(Error("Please enter a valid Total Volume")); } if (listing.CategoryId == Entities.Category.LorriesId || listing.CategoryId == Entities.Category.ProcessingId) { listing.AvailableWithFuel = listing.AvailableWithoutFuel = true; } var photos = new List <string>(); if (Model.Photos != null) { foreach (var photo in Model.Photos) { var filename = Entities.File.SaveBase64Image(photo.Base64); var file = new Entities.File(filename); file.Resize(200, 200, file.ThumbName); file.Resize(800, 800, file.ZoomName); photos.Add(filename); } } if (photos.Count > 0) { listing.PhotoPaths = string.Join(",", photos.ToArray()); } if (listing.Save()) { listing = Entities.Listing.Find(listing.Id); return(Success(new { Listing = listing.Json() })); } return(Error("An unknown error occurred")); }