Пример #1
0
        public async Task <IActionResult> GetToDoAsync(string userId, string todoId)
        {
            var UserIdfromTokken = HttpContext.User.FindFirst(p => p.Type == "sub").Value;

            if (UserIdfromTokken == null || userId != UserIdfromTokken)
            {
                var error = ServiceErrorResponses.AuthIsNotConfirmed("Unauthorized request");
            }

            var todoServ = new ToDoService(UserIdfromTokken);

            if (!int.TryParse(todoId, out var ToDoIdCheck))
            {
                var error = ServiceErrorResponses.ToDoNotFound(todoId);
                return(this.NotFound(error));
            }

            var todoIdGet = todoServ.Get(ToDoIdCheck);

            if (todoIdGet == null)
            {
                var error = ServiceErrorResponses.ToDoNotFound(todoId);
                return(this.NotFound(error));
            }

            return(this.Ok(todoIdGet));
        }
Пример #2
0
        public async Task <IActionResult> PatchToDoAsync([FromRoute] string userId, string todoId, [FromBody] ToDoUpdateInfo updInfo)
        {
            var UserIdfromTokken = HttpContext.User.FindFirst(p => p.Type == "sub").Value;

            if (UserIdfromTokken == null || userId != UserIdfromTokken)
            {
                var error = ServiceErrorResponses.AuthIsNotConfirmed("Unauthorized request");
            }

            var todoServ = new ToDoService(UserIdfromTokken);

            if (!int.TryParse(todoId, out var ToDoIdCheck))
            {
                var error = ServiceErrorResponses.ToDoNotFound(todoId);
                return(this.NotFound(error));
            }

            var todoIdPatch = todoServ.Get(ToDoIdCheck);

            if (todoIdPatch == null)
            {
                var error = ServiceErrorResponses.ToDoNotFound(todoId);
                return(this.NotFound(error));
            }

            var updated = false;

            if (updInfo.Text != null)
            {
                todoIdPatch.Text = updInfo.Text;
                updated          = true;
            }

            if (updInfo.IsDone != null)
            {
                todoIdPatch.IsDone = updInfo.IsDone.Value;
                updated            = true;
            }

            if (!updated)
            {
                var error = ServiceErrorResponses.BodyIsMissingOrUncorrect(updInfo.ToString());
                return(this.BadRequest(error));
            }

            todoIdPatch.LastUpdatedAt = DateTime.UtcNow;

            todoServ.Update(ToDoIdCheck, todoIdPatch);

            var clientToDo = ToDoConverter.Convert(todoIdPatch);

            return(this.Ok(clientToDo));
        }
Пример #3
0
        public async Task <IActionResult> SearchUserToDoListAsync(string userId)
        {
            var UserIdfromTokken = HttpContext.User.FindFirst(p => p.Type == "sub").Value;

            if (UserIdfromTokken == null || userId != UserIdfromTokken)
            {
                var error = ServiceErrorResponses.AuthIsNotConfirmed("Unauthorized request");
            }

            var todoServ = new ToDoService(UserIdfromTokken);

            var todoIdGet = todoServ.Get();

            if (todoIdGet.IsNullOrEmpty())
            {
                var error = ServiceErrorResponses.ToDoNotFound("this user");
                return(this.NotFound(error));
            }

            return(this.Ok(todoIdGet));
        }
Пример #4
0
        public async Task <IActionResult> CreateToDoAsync([FromRoute] string userId, [FromBody] ToDoBuildInfo todoNewInfo)
        {
            var UserIdfromTokken = HttpContext.User.FindFirst(p => p.Type == "sub").Value;

            if (UserIdfromTokken == null || userId != UserIdfromTokken)
            {
                var error = ServiceErrorResponses.AuthIsNotConfirmed("Unauthorized request");
            }

            var todoServ = new ToDoService(UserIdfromTokken);

            var creationInfo = ToDoBuildInfoConverter.Convert(userId, todoNewInfo);

            if (todoNewInfo == null)
            {
                throw new ArgumentNullException(nameof(todoNewInfo));
            }

            var id = todoServ.Get().Count + 1;
            var timeAtJustMoment = DateTime.UtcNow;

            var todoNew = new Models.ToDoList.ToDo
            {
                Id            = id,
                UserId        = userId,
                IsDone        = false,
                Text          = creationInfo.Text,
                CreatedAt     = timeAtJustMoment,
                LastUpdatedAt = timeAtJustMoment
            };

            var clientToDoInfo = ToDoInfoConverter.Convert(todoNew);

            todoServ.Create(todoNew);

            return(this.Ok($"Created at route: v1/todo/{userId}/{id}"));
        }