public async Task <Getlogin> GetloginUser() { var Authorization = Request.Headers["Authorization"].ToString(); var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:4000/api/auth/getlogin"); request.Headers.Add("Authorization", Authorization); var client = _clientFactory.CreateClient(); var response = await client.SendAsync(request); var data = await response.Content.ReadAsStringAsync(); if (response.StatusCode.ToString() == "OK") { var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }; Getlogin userinfo = System.Text.Json.JsonSerializer.Deserialize <TodoApi.Models.JsonModel.Getlogin>(data, options); return(userinfo); } return(null); }
public async Task <ActionResult <TodoItem> > GetTodoItem() { Getlogin userinfo = await GetloginUser(); var todoItems = _dbcontext.TodoItems .Where(b => b.UserId == userinfo.id).ToList(); return(Ok(todoItems)); }
public async Task <ActionResult <String> > CreateTodoItem(TodoItemAdd todoItemAdd) { Getlogin userinfo = await GetloginUser(); if (userinfo == null) { Response.StatusCode = 401; await Response.WriteAsync("Пользователь не авторизован"); } if (todoItemAdd.Title == null || todoItemAdd.Body == null) { return(BadRequest(new { message = "Title or body is incorrect" })); } // Получился очень странный запрос нужно с ним поработать)) var todoItems = _dbcontext.TodoItems .Where(b => b.OnCreate >= DateTime.Now && b.OnCreate <= DateTime.Now.AddDays(1) && b.Title.Contains(todoItemAdd.Title.ToString()) ).ToList(); if (todoItems.Count != 0) { return(BadRequest(new { message = "A task with the same Name already exists." })); } TodoItem item = new TodoItem() { Title = todoItemAdd.Title, Body = todoItemAdd.Body, UserId = userinfo.id, }; try{ _dbcontext.Add(item); _dbcontext.SaveChanges(); }catch (InvalidOperationException) { return(BadRequest("can't be created task")); } long statusId = await CreateItem(item.id); if (statusId == -1) { return(BadRequest("Status can't be saved")); } var response = new { id = item.id }; return(Ok(response)); }
public async Task <ActionResult <TodoItem> > DeleteTodoItem(int id) { Getlogin userinfo = await GetloginUser(); if (userinfo == null) { Response.StatusCode = 401; await Response.WriteAsync("Пользователь не авторизован"); } var todoItem = await _dbcontext.TodoItems.FindAsync(id); if (todoItem == null) { return(NotFound()); } if (userinfo.id == todoItem.UserId) { bool deleted = await DeleteItemStatus(todoItem.id); if (deleted) { _dbcontext.TodoItems.Remove(todoItem); await _dbcontext.SaveChangesAsync(); return(Ok()); } else { return(BadRequest(new { message = "Server isn't connected" })); } } Response.StatusCode = 403; await Response.WriteAsync("Ошибка доступа"); return(null); }
public async Task <ActionResult <String> > PutTodoItem(TodoItem changedItem, int id) { Getlogin userinfo = await GetloginUser(); if (userinfo == null) { Response.StatusCode = 401; await Response.WriteAsync("Пользователь не авторизован"); } TodoItem item = _dbcontext.TodoItems.FirstOrDefault(x => x.id == id); if (item == null) { return(BadRequest()); } item.Title = changedItem.Title; item.Body = changedItem.Body; _dbcontext.Entry(item).State = EntityState.Modified; try { await _dbcontext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TodoItemExists(id)) { return(BadRequest()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <TodoItem> > GetTodoItem(int id) { Getlogin userinfo = await GetloginUser(); var todoItem = await _dbcontext.TodoItems.FindAsync(id); if (todoItem == null) { return(NotFound()); } if (userinfo.id == todoItem.UserId) { var response = new { title = todoItem.Title, text = todoItem.Body, completed = GetItemStatus(id), created = todoItem.OnCreate }; return(Ok(response)); } return(StatusCode(403)); }