public async Task Create(NeuerBenutzerRequest request) { if (!string.IsNullOrEmpty(request.OeffentlicherName)) { if (!await _kontokorrentContext.Kontokorrent.AnyAsync(v => v.OeffentlicherName == request.OeffentlicherName)) { throw new KontokorrentNotFoundException(); } } if (!string.IsNullOrEmpty(request.EinladungsCode)) { if (!await _kontokorrentContext.EinladungsCode.AnyAsync(v => v.Id == request.EinladungsCode)) { throw new KontokorrentNotFoundException(); } } string hashedSecret; using (SHA256 sha256Hash = SHA256.Create()) { byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(request.Secret)); hashedSecret = Convert.ToBase64String(bytes); } await _kontokorrentContext.BenutzerSecret.AddAsync(new BenutzerSecret() { BenutzerId = request.Id, HashedSecret = hashedSecret }); await _kontokorrentContext.SaveChangesAsync(); var benutzerId = new Models.BenutzerID(request.Id); if (!string.IsNullOrEmpty(request.OeffentlicherName)) { await _kontokorrentsService.HinzufuegenPerOeffentlicherName(request.OeffentlicherName, benutzerId); } else if (!string.IsNullOrEmpty(request.EinladungsCode)) { await _kontokorrentsService.HinzufuegenPerCode(request.EinladungsCode, benutzerId); } }
public async Task <IActionResult> Neu([FromBody] NeuerBenutzerRequest request) { if (!ModelState.IsValid) { return(BadRequest()); } if (!string.IsNullOrEmpty(request.EinladungsCode) && !string.IsNullOrEmpty(request.OeffentlicherName)) { return(BadRequest("Entweder EinladungsCode oder OeffentlicherName angeben")); } if (await _userService.Exists(request.Id)) { return(BadRequest("Benutzer existiert")); } try { await _userService.Create(request); } catch (KontokorrentNotFoundException) { return(NotFound()); } return(Ok()); }