public async Task <IActionResult> Update([FromRoute] int id, [FromBody] TodoRequest request) { var user = await GetUser(); var todo = await _todoService.GetById(id); if (todo == null) { return(BadRequest("Invalid id")); } if (user.Id != todo.User.Id) { return(Forbid()); } todo.Title = request.Title; todo.Description = request.Description; todo.IsComplete = request.IsComplete; todo.LastUpdate = DateTime.UtcNow; var result = await _todoService.Update(todo); return(Ok(new TodoUpdateResponse(result))); }
public async Task <IActionResult> PutTodo(Guid id, TodoRequest todo) { if (id == Guid.Empty) { return(BadRequest()); } //MAP var todoToEdit = todo.Adapt <Todo>(); todoToEdit.Id = id; _context.Entry(todoToEdit).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TodoExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public static void PostNewTodoItem(TodoRequest payload) { var request = new RestRequest("/api/todo").AddJsonBody(payload); var response = new RestClient(AppUri).Post(request); Assert.Equal(201, (int)response.StatusCode); }
public async Task <TodoResponse> Update(int id, TodoRequest request) { var todo = await repository.Find(id); if (IsNotTodoAvalible(todo)) { return new TodoResponse { Error = "this todo is not found with this id:" + id } } ; if (!await categoryService.IsCategoryBelongsToThisUser(GetUserId(), request.CategoryId)) { return new TodoResponse { Error = "this category is unauthroized to use" } } ; todo.Body = request.Body; todo.CategoryId = request.CategoryId; todo.Completed = request.Completed; return(new TodoResponse { Todo = MapTodoEntity(await repository.Update(todo)) }); }
public async Task <TodoResponse> Create(TodoRequest request) { var userId = GetUserId(); if (!(await categoryService.IsCategoryBelongsToThisUser(userId, request.CategoryId))) { return new TodoResponse { Error = "This category does not belong to this user" } } ; var todo = await repository.Create(new Todo { Body = request.Body, Completed = request.Completed, CategoryId = request.CategoryId, UserId = userId }); return(new TodoResponse { Todo = MapTodoEntity(todo) }); }
/// <summary> /// Gets a collection of Todos. /// </summary> /// <param name="request">The Todo Request message</param> /// <param name="context">The context for the RPC call</param> /// <returns>A Todo Response containing a collection of available Todos</returns> public override Task <TodoResponse> GetTodos(TodoRequest request, ServerCallContext context) { var response = new TodoResponse(); response.Todos.AddRange(_todos.Where(t => t.UserId == request.UserId)); return(Task.FromResult(response)); }
public async Task <TodoResponse> Update(TodoRequest req) { var request = JsonConvert.SerializeObject(req); var httpResponse = await Client.PutAsync(Url, new StringContent(request, Encoding.Default, "application/json")); var response = await httpResponse.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <TodoResponse>(response)); }
public bool EditTodo(TodoRequest request) { try { return(_todoLogic.EditTodo(request)); } catch (Exception e) { throw new FaultException("The edition was failed"); } }
public TodoDto AddTodo(TodoRequest request) { try { return(_todoLogic.AddTodo(request).Map()); } catch (Exception e) { throw new FaultException("Adding to the database was failed"); } }
public IActionResult Create([FromBody] TodoRequest model) { var todoItem = new TodoItem { Name = model.Name !, IsComplete = model.IsComplete }; _context.TodoItems.Add(todoItem); _context.SaveChanges(); return(CreatedAtAction(nameof(GetById), new { id = todoItem.Id }, new TodoResponse(todoItem.Id, todoItem.Name, todoItem.IsComplete))); }
public async Task <ActionResult <Todo> > PostTodo(TodoRequest todo) { //VALIDATE //MAP var newTodo = todo.Adapt <Todo>(); _context.Todos.Add(newTodo); await _context.SaveChangesAsync(); return(CreatedAtAction("GetTodo", new { id = newTodo.Id }, newTodo)); }
public void TestAddNewTodoItem() { // given var newItem = new TodoRequest("yoga", false); // when TodoClients.PostNewTodoItem(newItem); // then List <TodoResponseItem> resultList = TodoClients.GetAllTodos(); Assert.True(ItemOperations.todoContains(newItem.Name, newItem.IsComplete, resultList)); }
internal static TodoModel BuildTodoRequestModel(TodoRequest request) { TodoModel todoModel = new() { Complete = request.Complete, Created = DateTime.Now.ToUniversalTime(), Description = request.Description }; return(todoModel); } }
public async Task <IActionResult> Create([FromBody] TodoRequest request) { var user = await GetUser(); if (user == null) { return(BadRequest()); } var todo = await _todoService.Create(request.Title, request.Description, user); return(CreatedAtRoute("Todos_GetById", new { id = todo.Id }, null)); }
public async Task <IActionResult> Update(int id, [FromForm] TodoRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var todo = await service.Update(id, request); if (todo.Status == Http.Responses.TodoResponse.StatusEnum.Fail) { return(NotFound(new { ClientError = todo.Error })); } return(Ok(todo)); }
public async Task <IActionResult> Create([FromForm] TodoRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var todo = await service.Create(request); if (todo.Status == Http.Responses.TodoResponse.StatusEnum.Fail) { return(BadRequest(new { ClientError = todo.Error })); } return(Created("", todo)); }
public IActionResult Add([FromBody] TodoRequest req) { try { var model = _mapper.Map <Todo>(req); model.Id = Guid.NewGuid(); model.CreatedAt = DateTime.UtcNow; Todos.Add(model); return(Created("", _mapper.Map <TodoResponse>(model))); } catch (Exception ex) { return(StatusCode(500, ex)); } }
public async Task <Todo> PostTodo(TodoRequest request) { var todoId = Guid.NewGuid().ToString().Split("-").First(); var todo = new Todo { Task = request.Task, Date = request.Date, Status = request.Status, TodoId = todoId }; var response = await _repository.PostTodo(todo); return(response); }
public ActionResult Create([FromBody] TodoRequest request) { try { var id = Guid.NewGuid().ToString(); _store.TryAdd(id, new TodoResponse { Id = id, Text = request.Text }); return(Ok(id)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, new { Error = "Internal Error" })); } }
public override Task <RPCResponse> InsertTodo(TodoRequest request, ServerCallContext context) { return(Task.Run(() => { if (request != null) { _list.Add(request.Model); return new RPCResponse { IsDone = true }; } return new RPCResponse { IsDone = false }; })); }
public override Task <TodoListResponse> TodoList(TodoRequest request, ServerCallContext context) { return(Task.FromResult( new TodoListResponse() { Items = { new TodoItem() { Title = "test", Description = "gRPC test" } } } )); }
public IActionResult Update([FromRoute] long id, [FromBody] TodoRequest model) { var todo = _context.TodoItems.Find(id); if (todo == null) { return(NotFound()); } todo.Name = model.Name !; todo.IsComplete = model.IsComplete; _context.TodoItems.Update(todo); _context.SaveChanges(); return(NoContent()); }
public override async Task <TodoReply> Add(TodoRequest request, ServerCallContext context) { Stopwatch stopwatch = new(); stopwatch.Start(); _logger.LogInformation("Creating new Todo {description}", request.Description); var todoModel = BuildTodoRequestModel(request); await _dataService.Add(todoModel, context.CancellationToken); var response = BuildTodoReply(todoModel); _logger.LogInformation("Completed creation for {id} in {duration}ms", todoModel.Id, stopwatch.ElapsedMilliseconds); stopwatch.Stop(); return(response); }
public async Task <string> Create(TodoRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } HttpContent content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync($"api/todos", content); if (response.IsSuccessStatusCode) { return(await response.Content.ReadAsStringAsync()); } return(null); //TODO Errors }
public override Task <RPCResponse> DeleteTodo(TodoRequest request, ServerCallContext context) { return(Task.Run(() => { if (request != null) { var todo = _list.FirstOrDefault(x => x.ID == request.Model.ID); if (todo != null) { _list.Remove(todo); } return new RPCResponse { IsDone = true }; } return new RPCResponse { IsDone = false }; })); }
public async Task <TodoDto> AddTodo(string title, string content, long projectId) { string uri = "api/todo/addtodo"; var request = new TodoRequest { Content = content, Title = title, ProjectId = projectId }; var addTodoToDb = await httpServices.Post <TodoDto, TodoRequest>(uri, request); if (addTodoToDb == null) { throw new Exception("Adding to the database was failed"); } return(addTodoToDb); }
public async Task <bool> EditTodo(long id, long statusId, string title, string content) { string uri = "api/todo/edittodo"; var request = new TodoRequest { Id = id, StatusId = statusId, Title = title, Content = content }; var editTodoInDb = await httpServices.Put(uri, request); if (editTodoInDb == false) { throw new Exception("The edition was failed"); } return(editTodoInDb); }
public bool EditTodo(TodoRequest request) { bool flag = false; using (var db = new TimeTrackerModelContainer()) { var result = db.Todo.First(x => x.Id == request.Id); result.Content = request.Content; result.StatusId = request.StatusId; result.Title = request.Title; result.TimeInSeconds = result.TimeInSeconds; result.Comment = result.Comment; db.Entry(result).State = EntityState.Modified; db.SaveChanges(); flag = true; } return(flag); }
public async Task <ActionResult <Todo> > PostTodo([FromBody] TodoRequest model) { var category = _context.Categories.Find(model.CategoryId); if (category == null) { return(BadRequest($"Category {model.CategoryId} not found")); } var todo = new Todo() { Title = model.Title, Details = model.Details, Done = model.Done, Category = category }; _context.Todos.Add(todo); await _context.SaveChangesAsync(); return(CreatedAtAction("GetTodo", new { id = todo.Id }, todo)); }
public Todo AddTodo(TodoRequest request) { var statusLogic = new StatusLogic(); var newtimerecordLogic = new TimeRecord(); var newTodo = new Todo { StatusId = statusLogic.GetStatus(States.New).Id, Content = request.Content, Title = request.Title, ProjectId = request.ProjectId, TimeInSeconds = request.TimeInSeconds, Comment = request.Comment }; using (var db = new TimeTrackerModelContainer()) { db.Todo.Add(newTodo); db.SaveChanges(); } return(newTodo); }