public async Task <ActionResult <RegistrationResponse> > RegisterAsync(RegistrationRequest request) { using (await _locker.EnterAsync(request.Username)) { // ensure username is unique if (await _users.GetByNameAsync(request.Username) != null) { return(BadRequest($"Cannot use the username '{request.Username}'.")); } var user = new User { Username = request.Username, Secret = _hash.Hash(request.Password), Permissions = _options.DefaultUserPermissions }; await _users.UpdateAsync(user); await _snapshot.CreatedAsync(user, default, SnapshotType.System, user.Id);
public async Task <ActionResult <Book> > DeleteUploadAsync(string id, [FromQuery] bool commit) { using (var task = _uploads.RemoveTask <BookUpload>(id)) { if (task == null) { return(ResultUtilities.NotFound <UploadTask>(id)); } if (!commit) { return(Ok()); } if (task.FileCount == 0) { return(BadRequest("No files were uploaded to be committed.")); } if (task.Data.Book != null) { var book = _mapper.Map <Book>(task.Data.Book); var content = _mapper.Map <BookContent>(task.Data.Content); content.Id = Snowflake.New; content.PageCount = task.FileCount; book.Contents = new[] { content }; await _books.UpdateAsync(book); await _snapshots.CreatedAsync(book); await UploadContentAsync(book, content, task); return(book); } if (task.Data.BookId != null) { using (await _locker.EnterAsync(task.Data.BookId)) { var book = await _books.GetAsync(task.Data.BookId); if (book == null) { return(ResultUtilities.NotFound <Book>(task.Data.BookId)); } var content = _mapper.Map <BookContent>(task.Data.Content); content.Id = Snowflake.New; content.PageCount = task.FileCount; book.Contents = book.Contents.Append(content).ToArray(); await _books.UpdateAsync(book); await _snapshots.ModifiedAsync(book); await UploadContentAsync(book, content, task); return(book); } } throw new InvalidOperationException("Invalid upload state."); } }