public async Task <ViewInformation> CreateViewAsync(CreateViewBody body, string submitter) { var id = body.ViewId; if (string.IsNullOrEmpty(body.ViewId)) { id = GenerateViewId(); } if (await ExistsAsync(id)) { throw new DocumentAlreadyExistsException("ViewID already exists"); } var view = new View(id, body.Query, submitter, body.Expires); await viewCollection.InsertOneAsync(view); return(new ViewInformation(id)); }
public async Task <IActionResult> Create([FromBody] CreateViewBody body) { if (FromArgumentIsNullOrContainsPlaceholder(body.Query)) { return(BadRequest("Data type (FROM-argument) must be specified in query and cannot be a placeholder")); } var dataType = DetermineViewCollection(body.Query); // Authroize var loggedInUsername = UsernameNormalizer.Normalize(HttpContext.User.Identity.Name); var resourceDescription = new CreateViewResourceDescription(dataType); var authorizationResult = await authorizationModule.AuthorizeAsync(resourceDescription, loggedInUsername); if (!authorizationResult.IsAuthorized) { return(StatusCode((int)HttpStatusCode.Unauthorized, "Not authorized")); } try { var viewInformation = await viewManager.CreateViewAsync(body, authorizationResult.User.UserName); apiEventLogger.Log(LogLevel.Info, $"User '{authorizationResult.User.UserName}' added view with ID '{viewInformation.ViewId}'"); return(new ContentResult { ContentType = Conventions.JsonContentType, Content = JsonConvert.SerializeObject(viewInformation), StatusCode = (int)HttpStatusCode.OK }); } catch (DocumentAlreadyExistsException) { return(Conflict($"View with name '{body.ViewId}' already exists")); } catch (Exception e) { return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message)); } }
private static HttpContent ConstructCreateViewBody(string query, DateTime expires, string viewId = null) { var createViewBody = new CreateViewBody(query, expires, viewId); return(PostBodyBuilder.ConstructBody(createViewBody)); }