public async Task <IActionResult> RegisterUser([FromBody] Client.Models.Users.UserRegistrationInfo registrationInfo, CancellationToken cancellationToken) { if (registrationInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("RegistrationInfo"); return(BadRequest(error)); } if (registrationInfo.Login == null || registrationInfo.Password == null) { var error = ServiceErrorResponses.NotEnoughUserData(); return(BadRequest(error)); } var creationInfo = new UserCreationInfo(registrationInfo.Login, Auth.AuthHash.GetHashPassword(registrationInfo.Password)); User user = null; try { user = await users.CreateAsync(creationInfo, cancellationToken); } catch (UserDuplicationException) { var error = ServiceErrorResponses.UserNameAlreadyExists(registrationInfo.Login); return(BadRequest(error)); } var clientUser = UserConverter.Convert(user); return(Ok(clientUser)); }
public async Task <IActionResult> CreateAsync([FromBody] Client.Maraphone.Task.ContentBuildInfo contentBuildInfo, CancellationToken cancellationToken) { if (contentBuildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("ContentBuildInfo"); return(this.BadRequest(error)); } var contentType = HttpContext.Request.Headers["Skima-Data-Type"].FirstOrDefault(); if (contentType == null) { return(this.BadRequest("Content not specified")); } var contentCreationInfo = new ContentCreationInfo(contentType, contentBuildInfo.Data); var document = await contentRepository.CreateAsync(contentCreationInfo, cancellationToken); var result = new ContentResult { Id = document.Id }; return(CreatedAtRoute( "GetContent", new { id = result.Id }, result)); }
public async Task <IActionResult> PatchNoteAsync([FromRoute] string noteId, [FromBody] Client.Notes.NotePatchInfo patchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (patchInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("NotePatchInfo"); return(this.BadRequest(error)); } if (!Guid.TryParse(noteId, out var noteIdGuid)) { var error = ServiceErrorResponses.NoteNotFound(noteId); return(this.NotFound(error)); } var modelPathInfo = NotePathcInfoConverter.Convert(noteIdGuid, patchInfo); Model.Notes.Note modelNote = null; try { modelNote = await this.repository.PatchAsync(modelPathInfo, cancellationToken).ConfigureAwait(false); } catch (Model.Notes.NoteNotFoundExcepction) { var error = ServiceErrorResponses.NoteNotFound(noteId); return(this.NotFound(error)); } var clientNote = NoteConverter.Convert(modelNote); return(this.Ok(clientNote)); }
public async Task <IActionResult> CreateTaskAsync([FromBody] Client.Models.ToDoTasks.ToDoTaskBuildInfo buildInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (buildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("ToDoTasksBuildInfo"); return(this.BadRequest(error)); } var userIdClame = this.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier); var modelCreationInfo = ToDoTaskBuildInfoConverter.Convert(userIdClame.Value, buildInfo); var modelTaskInfo = await this.tasks.CreateAsync(modelCreationInfo, cancellationToken); var clientTaskInfo = ToDoTaskInfoConverter.Convert(modelTaskInfo); var routeParams = new Dictionary <string, object> { { "taskId", clientTaskInfo.Id } }; return(this.CreatedAtRoute("GetTaskRouteV2", routeParams, clientTaskInfo)); }
public async Task <IActionResult> CreateAsync([FromBody] Client.Entry.EntryBuildInfo entryBuildInfo, CancellationToken cancellationToken) { if (entryBuildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("Entry"); return(this.BadRequest(error)); } var userId = User.FindFirstValue("userId"); if (userId == null) { var error = ServiceErrorResponses.InvalidClaims("userId"); return(this.BadRequest(error)); } var entryCreationInfo = new EntryCreationInfo(userId, entryBuildInfo.ActivityId); Entry modelEntry; try { modelEntry = await entryRepository.CreateAsync(entryCreationInfo, cancellationToken); } catch (EntryDuplicationException e) { return(BadRequest(e.Message)); } var clientEntry = EntryConverter.Convert(modelEntry); return(CreatedAtRoute( "GetEntry", new { id = clientEntry.Id }, clientEntry)); }
public async Task <IActionResult> UpdateAsync([FromBody] Client.Users.UserPatchInfo patchInfo, CancellationToken cancellationToken) { if (patchInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("UserPatchInfo"); return(this.BadRequest(error)); } var userId = User.FindFirstValue("userId"); if (userId == null) { var error = ServiceErrorResponses.InvalidClaims("userId"); return(this.BadRequest(error)); } User modelUser; try { modelUser = await userRepository.PatchAsync(patchInfo, userId, cancellationToken); } catch (UserNotFoundException e) { var error = ServiceErrorResponses.NoSuchObject("User", e.Message); return(this.NotFound(error)); } var clientUserInfo = UserConverter.ConvertToUserInfo(modelUser); return(Ok(clientUserInfo)); }
public async Task <IActionResult> CreateUserAsync([FromBody] Client.UserCreationInfo creationInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (creationInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("UserCreationInfo"); return(BadRequest(error)); } var hashPassword = PasswordEncoder.Encode(creationInfo.Password); var modelCreationInfo = new UserCreationInfo(creationInfo.Login, hashPassword); User modelUser = null; try { modelUser = await repository.CreateAsync(modelCreationInfo, cancellationToken) .ConfigureAwait(false); } catch (UserDuplicationException) { var error = ServiceErrorResponses.UserDuplication(modelCreationInfo.Login); return(BadRequest(error)); } var clientUser = UserConverter.Convert(modelUser); return(Ok(clientUser)); }
public async Task <IActionResult> GetUserByLoginAsync([FromRoute] string userLogin, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (userLogin == null) { var error = ServiceErrorResponses.BodyIsMissing(userLogin); return(BadRequest(error)); } User modelUser = null; try { modelUser = await repository.GetAsync(userLogin, cancellationToken).ConfigureAwait(false); } catch (UserNotFoundException) { var error = ServiceErrorResponses.UserNotFound(userLogin); return(NotFound(error)); } var clientUser = UserConverter.Convert(modelUser); return(Ok(clientUser)); }
public async Task <IActionResult> CreateTodoItemAsync([FromBody] Client.TodoCreationInfo creationInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (creationInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("TodoCreationInfo"); return(BadRequest(error)); } if (!HttpContext.Items.TryGetValue("userId", out var userId) || !Guid.TryParse(userId.ToString(), out var userGuid)) { var error = ServiceErrorResponses.UnAuthorized(); return(BadRequest(error)); } var modelCreationInfo = Converter.TodoCreationInfoConverter.Convert(userGuid, creationInfo); var modelTodoItem = await repository.CreateAsync(modelCreationInfo, cancellationToken).ConfigureAwait(false); var clientTodoItem = Converter.TodoItemConverter.Convert(modelTodoItem); var routeParams = new Dictionary <string, object> { { "todoId", clientTodoItem.Id } }; return(CreatedAtRoute("GetTodoItem", routeParams, clientTodoItem)); }
public async Task <IActionResult> CreateAsync([FromBody] Client.Maraphone.MaraphoneBuildInfo buildInfo, CancellationToken cancellationToken) { if (buildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("MaraphoneBuildInfo"); return(this.BadRequest(error)); } var userId = User.FindFirstValue("userId"); if (userId == null) { var error = ServiceErrorResponses.InvalidClaims("userId"); return(this.BadRequest(error)); } var maraphoneCreationInfo = new MaraphoneCreationInfo(buildInfo, userId); var modelMaraphone = await maraphoneRepository.CreateAsync(maraphoneCreationInfo, cancellationToken); var clientMaraphone = MaraphoneConverter.Convert(modelMaraphone); return(CreatedAtRoute( "GetMaraphone", new { id = clientMaraphone.Id }, clientMaraphone)); }
public async Task <IActionResult> CreateNoteAsync( [FromBody] Client.Notes.NoteBuildInfo clientBuildInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!this.TryGetSessionState(this.HttpContext.Request.Cookies, out var state)) { return(this.Unauthorized()); } if (!this.ModelState.IsValid) { var error = ServiceErrorResponses.BodyIsMissing(nameof(Client.Notes.NoteBuildInfo)); return(this.BadRequest(error)); } var modelBuildInfo = NoteBuildInfoConverter.Convert(state.UserId.ToString(), clientBuildInfo); var modelNoteInfo = await this.repository.CreateAsync(modelBuildInfo, cancellationToken).ConfigureAwait(false); var clientNoteInfo = NoteInfoConverter.Convert(modelNoteInfo); var routeParams = new Dictionary <string, object> { { "noteId", clientNoteInfo.Id } }; return(this.CreatedAtRoute("GetNoteRoute", routeParams, clientNoteInfo)); }
public async Task <IActionResult> Register([FromBody] Client.Users.UserRegistrationInfo registrationInfo, CancellationToken cancellationToken) { if (registrationInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("UserRegistrationInfo"); return(this.BadRequest(error)); } User result; var creationInfo = new UserCreationInfo(registrationInfo.Login, Authenticator.HashPassword(registrationInfo.Password), registrationInfo.FirstName, registrationInfo.LastName, registrationInfo.Email, registrationInfo.Phone); try { result = await userRepository.CreateAsync(creationInfo, cancellationToken); } catch (UserDuplicationException) { var error = ServiceErrorResponses.ConflictLogin(creationInfo?.Login); return(this.Conflict(error)); } var clientUser = UserConverter.Convert(result); return(this.Ok(clientUser)); }
public async Task <IActionResult> CreateNoteAsync([FromBody] ClientModels.Todos.TodoBuildInfo buildInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (buildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("buildInfo"); return(this.BadRequest(error)); } var nameIdentifier = this.HttpContext.User.Claims .FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier); var user = await _userRepository.GetAsync(nameIdentifier.Value, cancellationToken).ConfigureAwait(false); var creationInfo = TodoBuildInfoConverter.Convert(user.Id.ToString(), buildInfo); var modelTodoInfo = await _todoService.CreateAsync(creationInfo, cancellationToken).ConfigureAwait(false); var clientTodoInfo = TodoInfoConverter.Convert(modelTodoInfo); var routeParams = new Dictionary <string, object> { { "recordId", clientTodoInfo.Id } }; return(CreatedAtRoute("GetTodoRoute", routeParams, clientTodoInfo)); }
public async Task <IActionResult> PatchNoteAsync([FromRoute] string itemId, [FromBody] Client.TodoItems.TodoItemPatchInfo patchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (patchInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("ItemPatchInfo"); return(this.BadRequest(error)); } if (!Guid.TryParse(itemId, out var ItemIdGuid)) { var error = ServiceErrorResponses.ItemNotFound(itemId); return(this.NotFound(error)); } var modelPathInfo = TodoItemPathcInfoConverter.Convert(ItemIdGuid, patchInfo); Model.TodoItems.TodoItem modelItem = null; var userid = this.usersRepository.Get(User.Identity.Name).Id; try { modelItem = await this.itemsRepository.PatchAsync(modelPathInfo, cancellationToken, userid).ConfigureAwait(false); } catch (Model.TodoItems.TodoItemNotFoundExcepction) { var error = ServiceErrorResponses.ItemNotFound(itemId); return(this.NotFound(error)); } var clientItem = TodoItemConverter.Convert(modelItem); return(this.Ok(clientItem)); }
public async Task <IActionResult> CreateItemsAsync([FromBody] Client.TodoItems.TodoItemBuildInfo buildInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (buildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("TodoItemBuildInfo"); return(this.BadRequest(error)); } var name = User.Identity.Name; var usser = this.usersRepository.Get(name).Id; var creationInfo = TodoItemBuildInfoConverter.Convert(usser.ToString(), buildInfo); var modelItemInfo = await this.itemsRepository.CreateAsync(creationInfo, cancellationToken).ConfigureAwait(false); var clientItemInfo = TodoItemInfoConverter.Convert(modelItemInfo); var routeParams = new Dictionary <string, object> { { "itemId", clientItemInfo.Id } }; return(this.CreatedAtRoute("GetItemRoute", routeParams, clientItemInfo)); }
public async Task <IActionResult> GenerateToken([FromBody] Client.Models.Users.UserRegistrationInfo userInfo, [FromServices] IJwtSigningEncodingKey signingEncodingKey, CancellationToken cancellationToken) { if (userInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("UserInfo"); return(BadRequest(error)); } if (userInfo.Login == null || userInfo.Password == null) { var error = ServiceErrorResponses.NotEnoughUserData(); return(BadRequest(error)); } User user; try { user = await users.GetAsync(userInfo.Login, cancellationToken); } catch (UserNotFoundException) { var error = ServiceErrorResponses.UserNotFound(userInfo.Login); return(BadRequest(error)); } if (user.PasswordHash != Auth.AuthHash.GetHashPassword(userInfo.Password)) { var error = ServiceErrorResponses.IncorrectPassword(); return(BadRequest(error)); } var clientUser = UserConverter.Convert(user); var claims = new Claim[] { new Claim(ClaimTypes.Name, clientUser.Login), new Claim(ClaimTypes.NameIdentifier, clientUser.Id), }; var token = new JwtSecurityToken( //issuer: "ToDoTasksApp", //audience: "ToDoTasksClient", claims: claims, expires: DateTime.Now.AddMinutes(AuthOptions.LIFETIME), signingCredentials: new SigningCredentials(signingEncodingKey.GetKey(), signingEncodingKey.SigningAlgorithm) ); var encodedJwt = new JwtSecurityTokenHandler().WriteToken(token); return(Ok(new AuthTokenAnswer { Login = userInfo.Login, AccessToken = encodedJwt })); }
public async Task <IActionResult> RegisterUser([FromBody] Client.Models.Users.UserRegistrationInfo registrationInfo, [FromServices] IJwtSigningEncodingKey signingEncodingKey, CancellationToken cancellationToken) { if (registrationInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("RegistrationInfo"); return(BadRequest(error)); } if (registrationInfo.Login == null || registrationInfo.Password == null) { var error = ServiceErrorResponses.NotEnoughUserData(); return(BadRequest(error)); } var creationInfo = new UserCreationInfo(registrationInfo.Login, Auth.AuthHash.GetHashPassword(registrationInfo.Password)); User user = null; try { user = await users.CreateAsync(creationInfo, cancellationToken); } catch (UserDuplicationException) { var error = ServiceErrorResponses.UserNameAlreadyExists(registrationInfo.Login); return(BadRequest(error)); } var clientUser = UserConverter.Convert(user); var claims = new Claim[] { new Claim(ClaimTypes.Name, clientUser.Login), new Claim(ClaimTypes.NameIdentifier, clientUser.Id), }; var encodedJwt = new JwtSecurityTokenHandler().WriteToken(JWT.GetJWT(claims, signingEncodingKey)); return(Ok(new AuthTokenAnswer { AccessToken = encodedJwt })); }
public async Task <IActionResult> PatchNoteAsync( [FromRoute] string noteId, [FromBody] Client.Notes.NotePatchInfo patchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!this.TryGetSessionState(this.HttpContext.Request.Cookies, out var state)) { return(this.Unauthorized()); } if (patchInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("NotePatchInfo"); return(this.BadRequest(error)); } if (!Guid.TryParse(noteId, out var noteIdGuid)) { var error = ServiceErrorResponses.NoteNotFound(noteId); return(this.NotFound(error)); } if (!this.IsNoteBelongsToUserAsync(state, noteIdGuid, cancellationToken).Result) { return(this.Forbid()); } Models.Notes.Note modelNote = null; var modelPathInfo = NotePathcInfoConverter.Convert(noteIdGuid, patchInfo); try { modelNote = await this.repository.PatchAsync(modelPathInfo, cancellationToken).ConfigureAwait(false); } catch (Models.Notes.NoteNotFoundExcepction) { var error = ServiceErrorResponses.NoteNotFound(noteId); return(this.NotFound(error)); } var clientNote = NoteConverter.Convert(modelNote); return(this.Ok(clientNote)); }
public IActionResult Register([FromBody] UserRegistrationInfo userRegistrationInfo) { if (!this.ModelState.IsValid) { var error = ServiceErrorResponses.BodyIsMissing(nameof(UserRegistrationInfo)); return(this.BadRequest(error)); } RegisterResult result = null; try { result = this.registrator.Register(userRegistrationInfo.Login, userRegistrationInfo.Password); } catch (RegistrationException ex) { return(this.BadRequest(ex.Message)); } return(this.Ok(result)); }
public async Task <IActionResult> GenerateToken([FromBody] Client.Models.Users.UserRegistrationInfo userInfo, [FromServices] IJwtSigningEncodingKey signingEncodingKey, CancellationToken cancellationToken) { if (userInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("UserInfo"); return(BadRequest(error)); } if (userInfo.Login == null || userInfo.Password == null) { var error = ServiceErrorResponses.NotEnoughUserData(); return(BadRequest(error)); } User user; try { user = await users.GetAsync(userInfo.Login, cancellationToken); } catch (UserNotFoundException) { var error = ServiceErrorResponses.UserNotFound(); return(BadRequest(error)); } var clientUser = UserConverter.Convert(user); var claims = new Claim[] { new Claim(ClaimTypes.Name, clientUser.Login), new Claim(ClaimTypes.NameIdentifier, clientUser.Id), }; var encodedJwt = new JwtSecurityTokenHandler().WriteToken(JWT.GetJWT(claims, signingEncodingKey)); return(Ok(new AuthTokenAnswer { AccessToken = encodedJwt })); }
public ActionResult <string> Post([FromBody] UserRegistrationInfo authRequest, [FromServices] IJwtSigningEncodingKey signingEncodingKey, CancellationToken cancellationToken) { if (authRequest == null) { var error = ServiceErrorResponses.BodyIsMissing(nameof(authRequest)); return(BadRequest(error)); } var user = _userRepository.GetAsync(authRequest.Login, cancellationToken); if (user.Result == null) { var error = ServiceErrorResponses.UserNotFound(authRequest.Login); return(BadRequest(error)); } if (user.Result.PasswordHash != Authenticator.HashPassword(authRequest.Password)) { var error = ServiceErrorResponses.IncorrectPassword(); return(BadRequest(error)); } var claims = new Claim[] { new Claim(ClaimTypes.NameIdentifier, authRequest.Login), }; var token = new JwtSecurityToken( issuer: "TodoListApp", audience: "Client", claims: claims, expires: DateTime.Now.AddMinutes(10), signingCredentials: new SigningCredentials( signingEncodingKey.GetKey(), signingEncodingKey.SigningAlgorithm) ); string jwtToken = new JwtSecurityTokenHandler().WriteToken(token); return(jwtToken); }
public async Task <IActionResult> PatchTodoItemAsync([FromRoute] string todoId, [FromBody] ClientModels.TodoItems.TodoPatchInfo patchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (patchInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("TodoPatchInfo"); return(BadRequest(error)); } if (!Guid.TryParse(todoId, out var todoItemGuid)) { var error = ServiceErrorResponses.TodoNotFound(todoId); return(NotFound(error)); } if (!HttpContext.Items.TryGetValue("userId", out var userId) || !Guid.TryParse(userId.ToString(), out var userGuid)) { var error = ServiceErrorResponses.UnAuthorized(); return(BadRequest(error)); } var modelPathInfo = ModelsConverters.TodoItems.TodoPatchConverter.Convert(todoItemGuid, userGuid, patchInfo); Models.TodoItems.TodoItem modelTodoItem = null; try { modelTodoItem = await repository.PatchAsync(modelPathInfo, cancellationToken).ConfigureAwait(false); } catch (Model.TodoNotFoundException) { var error = ServiceErrorResponses.TodoNotFound(todoId); return(NotFound(error)); } var clientTodoItem = ModelsConverters.TodoItems.TodoItemConverter.Convert(modelTodoItem); return(Ok(clientTodoItem)); }
public async Task <IActionResult> PatchNoteAsync([FromRoute] string recordId, [FromBody] ClientModels.Todos.TodoPatchInfo patchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (patchInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("RecordPatchInfo"); return(this.BadRequest(error)); } if (!Guid.TryParse(recordId, out var recordIdGuid)) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(this.NotFound(error)); } if (!await IsSameUser(recordIdGuid, cancellationToken)) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(NotFound(error)); } var modelPatchInfo = TodoPathcInfoConverter.Convert(recordIdGuid, patchInfo); Models.Todos.Todo modelRecord; try { modelRecord = await this._todoService.PatchAsync(modelPatchInfo, cancellationToken).ConfigureAwait(false); } catch (RecordNotFoundException) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(this.NotFound(error)); } var clientNote = TodoConverter.Convert(modelRecord); return(this.Ok(clientNote)); }
public async Task <IActionResult> CreateAsync([FromBody] Client.ActivityBuildInfo buildInfo, CancellationToken cancellationToken) { if (buildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("ActivityCreationInfo"); return(this.BadRequest(error)); } var userId = User.FindFirstValue("userId"); if (userId == null) { var error = ServiceErrorResponses.InvalidClaims("userId"); return(this.BadRequest(error)); } var maraphoneId = buildInfo.MaraphoneId; var maraphone = await maraphoneRepository.GetAsync(maraphoneId, cancellationToken); if (maraphone == null) { var error = ServiceErrorResponses.NoSuchObject("Maraphone", "Maraphone not found"); return(this.NotFound(error)); } var duration = maraphone.Duration; var endAt = buildInfo.StartAt + duration; var activityCreationInfo = new ActivityCreationInfo(buildInfo.MaraphoneId, buildInfo.Tags, userId, buildInfo.Experts, buildInfo.StartAt, endAt); var modelActivity = await activityRepository.CreateAsync(activityCreationInfo, endAt, cancellationToken); var clientActivity = ActivityConverter.Convert(modelActivity); return(CreatedAtRoute( "GetActivity", new { id = clientActivity.Id }, clientActivity)); }
Register([FromBody] UserRegistrationInfo registrationInfo, CancellationToken cancellationToken) { if (registrationInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("TodoRegistrationInfo"); return(BadRequest(error)); } var info = new UserCreationInfo(registrationInfo.Login, Authenticator.HashPassword(registrationInfo.Password)); try { await _userRepository.CreateAsync(info, cancellationToken); } catch (UserDuplicationException exception) { return(BadRequest(exception)); } return(Ok()); }
public async Task <IActionResult> CreateBatchAsync( [FromBody] BatchBirdsBuildInfo batchBuildInfo, CancellationToken token) { token.ThrowIfCancellationRequested(); if (batchBuildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("BatchBirdsBuildInfo"); return(this.BadRequest(error)); } var modelBatchBuildInfo = batchBuildInfo.Items.Select(BirdsConverter.Convert).ToList(); var modelBirds = await this.birdsRepository.CreateBatchAsync(modelBatchBuildInfo, token).ConfigureAwait(false); var viewBirdList = new BirdsList { Birds = modelBirds.Select(BirdsConverter.Convert).ToList(), }; return(this.Ok(viewBirdList)); }
public async Task <IActionResult> CreateTaskAsync([FromBody] Client.Models.ToDoTasks.ToDoTaskBuildInfo buildInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (buildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("ToDoTasksBuildInfo"); return(this.BadRequest(error)); } var userLoginRequest = this.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name); User user = null; try { user = await users.GetAsync(userLoginRequest.Value, cancellationToken); } catch { var error = ServiceErrorResponses.UserNotFound(userLoginRequest.Value); return(BadRequest(error)); } //добавить try-catch в случае если аргументы null var modelCreationInfo = ToDoTaskBuildInfoConverter.Convert(user.Id.ToString(), buildInfo); var modelTaskInfo = await this.tasks.CreateAsync(modelCreationInfo, cancellationToken); var clientTaskInfo = ToDoTaskInfoConverter.Convert(modelTaskInfo); var routeParams = new Dictionary <string, object> { { "taskId", clientTaskInfo.Id } }; return(this.CreatedAtRoute("GetTaskRoute", routeParams, clientTaskInfo)); }
public async Task <IActionResult> CreateNoteAsync([FromBody] Client.Notes.NoteBuildInfo buildInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (buildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("NoteBuildInfo"); return(this.BadRequest(error)); } var userId = Guid.Empty.ToString(); // Нужно исправить var creationInfo = NoteBuildInfoConverter.Convert(userId, buildInfo); var modelNoteInfo = await this.repository.CreateAsync(creationInfo, cancellationToken).ConfigureAwait(false); var clientNoteInfo = NoteInfoConverter.Convert(modelNoteInfo); var routeParams = new Dictionary <string, object> { { "noteId", clientNoteInfo.Id } }; return(this.CreatedAtRoute("GetNoteRoute", routeParams, clientNoteInfo)); }
public IActionResult Auth([FromBody] UserAuthenticationInfo authenticationInfo) { if (!this.ModelState.IsValid) { var error = ServiceErrorResponses.BodyIsMissing(nameof(UserAuthenticationInfo)); return(this.BadRequest()); } SessionState sessionState; try { sessionState = this.authenticator.AuthenticateAsync(authenticationInfo.Login, authenticationInfo.Password, new CancellationToken()).Result; } catch (AuthenticationException) { return(this.Unauthorized()); } this.HttpContext.Response.Cookies.Append("user_id", sessionState.UserId.ToString()); this.HttpContext.Response.Cookies.Append("pass_hash", sessionState.PasswordHash); this.HttpContext.Response.Cookies.Append("session_id", sessionState.SessionId); return(this.Ok()); }