public async Task <IActionResult> UpdateCar(int id, [FromBody] SaveCarResource savecarResource) { // Validations- check if not ModelState is valid if (!ModelState.IsValid) { // if it is invalid we return BadRequest return(BadRequest(ModelState)); } var car = await repository.GetCar(id); // if car is null instead of letting the application crash if (car == null) { // we return a proper response to the user return(NotFound()); } // Map API resource to Domain Object mapper.Map <SaveCarResource, Car>(savecarResource, car); // setting the last update property car.LastUpdate = DateTime.Now; // saving the changes await unitOfWork.CompleteAsync(); // reset the car object car = await repository.GetCar(car.Id); // map the result back to a CarResource var result = mapper.Map <Car, CarResource>(car); // return Ok (carResource); // Ok() inherited from the base controller class. Will return it with HTTP Status 200 return(Ok(result)); }
public async Task <IActionResult> CreateCar([FromBody] SaveCarResource carResource) { // Validations- check if not ModelState is valid // if (!ModelState.IsValid) // return BadRequest(ModelState); // Map API resource to Domain Object var car = mapper.Map <SaveCarResource, Car>(carResource); // setting the last update property car.LastUpdate = DateTime.Now; // Add the Car object into the dbcontext repository.Add(car); // saving the changes await unitOfWork.CompleteAsync(); // fetch a complete representation of a Car car = await repository.GetCar(car.Id); // map the result back to a CarResource var result = mapper.Map <Car, CarResource>(car); // return Ok (carResource); // Ok() inherited from the base controller class. Will return it with HTTP Status 200 return(Ok(result)); }
public async Task <IActionResult> Update(int id, [FromBody] SaveCarResource carResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var car = await _CarRepository.Get(id, loadExternal : true); _mapper.Map <SaveCarResource, Car>(carResource, car); car.lasUpdate = DateTime.Now; await _UnitOfWork.Complete(); car = await _CarRepository.Get(car.Id, loadExternal : true); return(Ok(_mapper.Map <Car, CarResource>(car))); }
public async Task <IActionResult> CreateVehicle([FromBody] SaveCarResource vehicleResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var vehicle = mapper.Map <SaveCarResource, Car>(vehicleResource); repository.AddVehicle(vehicle); await unitOfWork.CompleteAsync(); vehicle = await repository.GetVehicle(vehicle.Id); var result = mapper.Map <Vehicle, CarResource>(vehicle); return(Ok(result)); }
public async Task <IActionResult> PutAsync(int id, [FromBody] SaveCarResource resource) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } var car = _mapper.Map <SaveCarResource, Car>(resource); var result = await _service.UpdateAsync(id, car); if (!result.Success) { return(BadRequest(result.Message)); } var carResource = _mapper.Map <Car, CarResource>(result.Car); return(Ok(carResource)); }
public async Task <(SaveAdvertResource, SaveCarResource)> GetCarAdData(string url) { var config = Configuration.Default.WithDefaultLoader(); var context = BrowsingContext.New(config); var document = await context.OpenAsync(url); if (document.QuerySelector("title").TextContent == "craigslist | Page Not Found") { return(null, null); } SaveAdvertResource advert = new SaveAdvertResource(); SaveCarResource car = new SaveCarResource(); advert.Url = url; var dateTime = document.QuerySelector("time.date.timeago"); if (dateTime != null) { advert.DatePosted = DateTime.Parse(dateTime.GetAttribute("datetime")); } var description = document.QuerySelector("#postingbody"); if (description != null) { description.QuerySelector("div.print-information.print-qrcode-container").Remove(); advert.Description = description.TextContent.Trim(); } var price = document.QuerySelector("span.price"); if (price != null) { car.Price = uint.Parse(price.TextContent, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.AllowCurrencySymbol); } var attributeGroup = document.QuerySelectorAll("p.attrgroup"); if (attributeGroup != null && attributeGroup.Length > 0) { MatchCollection matches = Regex.Matches(attributeGroup[0].TextContent, @"\d{4}"); var year = uint.Parse(matches[0].ToString()); car.Year = year; matches = Regex.Matches(attributeGroup[0].TextContent, @"[^\s\d].+"); car.MakeModel = matches[0].ToString(); var attributes = attributeGroup[1].QuerySelectorAll("span"); foreach (var row in attributes) { string[] rowSplit = row.TextContent.Split(": "); switch (rowSplit[0]) { case "odometer": car.Miles = uint.Parse(rowSplit[1]); break; case "paint color": car.Color = rowSplit[1]; break; case "condition": car.Condition = rowSplit[1]; break; } } } return(advert, car); }