public async Task <IActionResult> Post([FromBody] RegisteredApiUser newUser) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //small controller thats only used internally, no need to over-engineer it with DTO var generatedApiKey = Guid.NewGuid(); var apiuser = new RegisteredApiUser { ApiKey = generatedApiKey.ToString(), Name = newUser.Name, Dns = newUser.Dns }; await _unitOfWork.RegisteredApiUsers.AddAsync(apiuser); await _unitOfWork.CompleteAsync(); return(CreatedAtAction(nameof(GetById), new { apikey = apiuser.ApiKey }, new { message = "Api User added successfully..." })); } catch (Exception ex) { //controller used internally, we can reveal what the hack happened :) return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }
public async Task <IActionResult> Put(string apikey, [FromBody] RegisteredApiUser oldUser) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var apiuser = (await _unitOfWork.RegisteredApiUsers.FindAsync(x => x.ApiKey == apikey)).FirstOrDefault(); if (apiuser == null) { return(NotFound(new { message = "Please check the API Key and submit again" })); } apiuser.Name = oldUser.Name; await _unitOfWork.CompleteAsync(); return(Ok(new { message = "Api User updated successfully..." })); } catch (Exception ex) { //controller used internally, we can reveal what the hack happened :) return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }