//createpartner;name;town;address;VAT public string Execute(IReadOnlyList <string> parameters) { int vat; bool isInt; Partner partner; Town town; Address address; List <char> number; string numberString; try { switch (parameters.Count) { case 1: //name partner = this.partnerService.Add(parameters[0]); return($"Partner {partner.Name} was created."); case 2: //name;VAT number = parameters[1].ToList().Skip(2).ToList(); numberString = string.Join("", number); isInt = int.TryParse(numberString, out vat); if (isInt && number.Count == 9) { partner = this.partnerService.Add(parameters[0], vat: parameters[1]); return($"Partner {partner.Name} with VAT: {partner.VAT} was created."); } else { throw new ArgumentException("Please provide a valid VAT number!"); } case 3: //name;town;adddress town = this.townService.GetTown(parameters[1]); address = this.addressSevice.GetAddress(town, parameters[2]); partner = partnerService.Add(parameters[0], address); return($"Partner {partner.Name} with address: {address.Text} in town: {town.Name} was created."); case 4: //name;town;address;vat number = parameters[3].ToList().Skip(2).ToList(); numberString = string.Join("", number); isInt = int.TryParse(numberString, out vat); if (!isInt) { throw new ArgumentException("Please provide a valid VAT number!"); } town = this.townService.GetTown(parameters[1]); address = this.addressSevice.GetAddress(town, parameters[2]); partner = partnerService.Add(parameters[0], address, parameters[3]); return($"Partner: {partner.Name} with address: {address.Text} in town: {town.Name} and VAT: {partner.VAT} was created."); default: throw new ArgumentException(@"Please provide parameter(s): Partner;[Town;Address];[VAT]"); } } catch (ArgumentException ex) { throw new ArgumentException(ex.Message + "\n\rNew partner was NOT created!"); } }
// [Authorize] public async Task <IActionResult> Post([FromBody] PartnerAddViewModel model) { try { if (ModelState.IsValid) { var savedPartner = service.Add(model.Partner, model.VehicleId); //AddAsync ? if (savedPartner != null) { return(Created($"/api/partner/{savedPartner.Id}", savedPartner)); } else { logger.LogError("savedPartner was null"); return(BadRequest(ModelState)); } } else { logger.LogError("ModelState is not valid"); return(BadRequest(ModelState)); } } catch (Exception ex) { logger.LogError($"Failed to save new partner: {ex}"); return(BadRequest($"Failed to save new partner beacuse of exception ")); } }
public IActionResult Post([FromBody] CreatePartnerViewModel newPartner) { try { _partnerService.Add(newPartner.Name, newPartner.BusinessName, newPartner.BusinessNumber); return(StatusCode(201)); } catch (System.Exception ex) { return(StatusCode(500, ex.Message)); } }
public IActionResult Add([FromBody] dynamic value) { var name = ""; try { string svalue = Convert.ToString(value); dynamic partnerJsonEntity = JsonConvert.DeserializeObject(svalue); name = partnerJsonEntity["name"].Value; var location = partnerJsonEntity["location"].Value; var addResult = _partnerService.Add(new ent.manager.Entity.Model.Partner() { Name = name, Location = location }); if (addResult) { return(Json(new { c = ResultCode.Success, d = "" })); } else { return(Json(new { c = ResultCode.PartnerResultCodes.PartnerFailedToAdd, d = "" })); } } catch (Exception ex) { _logger.LogError(ex.GetLogText("partnerapi_add")); var partner = _partnerService.GetByName(name); if (partner != null) { return(Json(new { c = ResultCode.PartnerResultCodes.PartnerNameAlreadyExists, d = ex.Message })); } return(Json(new { c = ResultCode.GenericException, d = ex.Message })); } }