public IActionResult UpdClient(int clientNumber, [FromBody] ClientIncDTO clientIncDTO) { if (clientIncDTO == null) { return(BadRequest(ModelState)); } // Eerst Id ophalen, en daarna de client via Id te updaten, werkt niet. // Als je een Update op Id doet NA een lees-aktie op Id, krijg je de volgende melding: // 'The instance of entity type 'Client' cannot be tracked because // another instance with the same key value for {'Id'} is already being tracked. var client = _mapper.Map <Client>(clientIncDTO); // De landencode kan zijn veranderd. Haal het land op var country = _countryRepository.GetCountryByIsoCode(clientIncDTO.CountryCodeISO); if (country != null) // land niet gevonden op basis van ISO code { client.CountryId = country.Id; } else // Land is optioneel, geen land toevoegen { client.CountryId = null; } // Tabel met nummer - reeksen kent maar 1 record: var success = _clientRepository.UpdateByClientNumber(client); if (!success) { ModelState.AddModelError("", $"Er is iets foutgegaan bij update voor {clientIncDTO.FirstName}"); return(StatusCode(500, ModelState)); } // Best practice bij een [HttpPost]: gebruik CreatedAtAction en geef het hele record terug aan Postman // De aanvrager kan bijv 3 velden sturen, en een record van 10 velden terug krijgen var clientOutgDto = _mapper.Map <ClientOutgDTO>(client); return(CreatedAtAction("GetByClientNr", "ClientApi", new { clientNumber = client.ClientNumber }, clientOutgDto)); }
public IActionResult CrtClient([FromBody] ClientIncDTO clientIncDTO) { if (clientIncDTO == null) { return(BadRequest(ModelState)); } // Converteer Data Transfer Object naar intern Client Object: var client = _mapper.Map <Client>(clientIncDTO); client.DoctorId = 1; // Dokter Bernard client.ActionCode = 'A'; // Haal het land op var country = _countryRepository.GetCountryByIsoCode(clientIncDTO.CountryCodeISO); if (country != null) // land niet gevonden op basis van ISO code { client.CountryId = country.Id; } else // Nederland toekennen als land { client.CountryId = Const.cCountryIdNederland; } // Zorg ervoor dat alle niet-nullable velden gevuld zijn voordat // je het record aan de database toevoegt try { // Tabel met nummer-reeksen kent maar 1 record: var newClientNumber = _clientRepository.GetAndUpdateCurrentClientNumber(); client.ClientNumber = newClientNumber; _clientRepository.Add(client); } catch (Exception errMsg) { ModelState.AddModelError("", $"Er is iets fout gegaan \n {errMsg}"); return(StatusCode(500, ModelState)); } // Best practice bij een [HttpPost]: gebruik CreatedAtAction en // geef het hele Outg record terug aan Postman. var clientOutgDto = _mapper.Map <ClientOutgDTO>(client); // Derived fields: doctorFullName / totalAmountOfInvoices / nrOfActiveInvoices DetermineDerivedFieldsPerClient(client); clientOutgDto.TotalAmountOfInvoices = totalAmountOfInvoices; clientOutgDto.NrOfActiveInvoices = nrOfActiveInvoices; clientOutgDto.DoctorFullName = doctorFullName; clientOutgDto.CountryDescription = countryDescription; // Stuur een outgoing DTO terug, daarin staan niet de technische velden DoctorId, ClientId, ActionCode //return CreatedAtAction("GetByClientNr", "ClientApi", // new { clientNumber = client.ClientNumber }, clientOutgDto); // return CreatedAtRoute(nameof(GetByClientNr), new { id = 1, version = ApiVersion.ToString() }, "test"); // return CreatedAtRoute("GetByClientNr", new { clientNumber = clientOutgDto.ClientNumber }, clientOutgDto); return(CreatedAtAction("GetByClientNr", "ClientApi", new { clientNumber = client.ClientNumber }, clientOutgDto)); }