public async Task <IActionResult> Delete(int buildingId, int userId) { //Get header token if (Request.Headers.TryGetValue("Authorization", out StringValues headerValues) && buildingId > -1 && userId > -1) { var token = _customEncoder.DecodeBearerAuth(headerValues.First()); if (token != null) { var user = await _userService.GetUserAsyncByToken(token); if (user != null) { //Verify if the token exist and is not expire if ((await _authenticationService.CheckIfTokenIsValidAsync(token) && user.IsAdmin == 1) || await _authenticationService.CheckIfTokenIsValidAsync(token, user.UserId)) { var isDeleted = await _buildingService.DeleteBuildingAsync(buildingId, userId, user.IsAdmin); if (isDeleted == false) { return(StatusCode(404, "Unable to delete building.")); } return(StatusCode(204, "Building has been deleted.")); } return(StatusCode(401, "Invalid token.")); } return(StatusCode(403, "Invalid user.")); } return(StatusCode(401, "Invalid authorization.")); } return(StatusCode(401, "Invalid authorization.")); }
public async Task <IActionResult> Delete(int id) { try { await _buildingService.DeleteBuildingAsync(id); return(RedirectToAction("list", "building")); } catch (Exception e) { if (e is FormatException || e is NullReferenceException || e is KeyNotFoundException) { Console.WriteLine(e.Message); return(NotFound()); } throw; } }