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));
        }
Exemple #2
0
        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));
        }