public static void ModifyItem(TodoModel Todo) { using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString())) { cnn.Execute("update Todo set Color = @Color, Priority = @Priority , TodoText = @TodoText, IsChecked = @IsChecked WHERE Tag=@Tag", Todo); } }
public List <Category> GetCategories() { using (TodoModel context = new TodoModel()) { return(context.Categories.ToList()); } }
public void AddTodo() { IsEditNow = true; EditTodoModel = new TodoModel { State = -1, IdPriority = 1 }; }
/// <summary> /// Get record by id /// </summary> /// <param name="connectString"></param> /// <param name="id"></param> /// <returns></returns> public TodoModel GetById(string connectString, int id) { SqlDataReader reader = null; SqlConnection myConnection = new SqlConnection(); myConnection.ConnectionString = connectString; SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "Select * from Todo where Id=" + id + " AND DeleteDate IS NULL"; sqlCmd.Connection = myConnection; myConnection.Open(); reader = sqlCmd.ExecuteReader(); TodoModel md = null; while (reader.Read()) { md = new TodoModel(); md.Id = Convert.ToInt32(reader.GetValue(0)); md.Name = reader.GetValue(1).ToString(); md.Description = reader.GetValue(2).ToString(); if (!string.IsNullOrEmpty(reader.GetValue(5).ToString())) { md.Ordering = Convert.ToInt32(reader.GetValue(5)); } } return md; }
public void GivenExistingTodo_DeleteByIndex_RepositoryDeleteRemoveCorrectTodo() { var mockTodos = new TodoModel[] { new TodoModel() { Id = Guid.Parse("0a000300-0600-0000-0100-0000f0700001"), Title = "Test", Description = "Test Description", Status = TodoStatus.Open, DueDate = new DateTime(2021, 3, 4, 12, 30, 00), CreatedOn = new DateTime(2020, 11, 12, 11, 55, 13), }, new TodoModel() { Id = Guid.Parse("0a000300-0600-0020-0100-0000f0300002"), Title = "Second", Description = "Second Test", Status = TodoStatus.InProgress, DueDate = new DateTime(2021, 1, 24, 1, 22, 33), CreatedOn = new DateTime(2020, 10, 11, 12, 13, 14), }, }; int index = 1; MockRepository.Setup(a => a.GetAll()).Returns(mockTodos); MockRepository.Setup(a => a.GetById(mockTodos[index].Id)).Returns(mockTodos[index]); MockRepository.Setup(a => a.Delete(mockTodos[index].Id)); var service = new Service.TodoService(MockRepository.Object); var isDeleted = service.DeleteByIndex(index); MockRepository.Verify(a => a.Delete(mockTodos[index].Id), Times.Once); }
public BaseResponse <string> Delete(TodoModel todoModel) { var response = new BaseResponse <string>(); response = _iTodoBusinesslogic.Delete(todoModel); return(response); }
public async Task <IActionResult> Update(TodoViewModel viewModel, string returnUrl = "") { if (ModelState.IsValid) { var model = (await _session.Query <TodoModel>().ListAsync()) .Where(m => m.TodoId == viewModel.TodoId) .FirstOrDefault(); if (model == null) { model = new TodoModel() { TodoId = viewModel.TodoId }; } model.Text = viewModel.Text; model.DueDate = viewModel.DueDate; model.IsCompleted = viewModel.IsCompleted; _session.Save(model); if (Url.IsLocalUrl(returnUrl)) { return(Redirect(returnUrl)); } return(RedirectToAction("Index", "Todo")); } return(View(viewModel)); }
[InlineData("600e0400-3c00-0000-3000-020050000001", "Football", "", TodoStatus.InProgress, "2020-05-15T14:29:15.1823029Z", "2020-05-19T21:00:00.0000000Z")] // without Description public void GivenNotExistedId_Update_DoesNothing(string id, string title, string description, TodoStatus status, string createdOn, string dueDate) { //arrange var todo = new TodoModel() { Id = Guid.Parse(id), Title = title, Description = description, Status = status, CreatedOn = DateTime.Parse(createdOn), DueDate = DateTime.Parse(dueDate) }; var repo = new Xml.TodoRepository(MockXmlContext.Object); //act todo.Title = "Concert"; todo.Description = "Go to Metallica concert"; repo.Update(todo); //assert var all = Container.Elements(); var element = all.FirstOrDefault(a => a.Attribute("Id").Value == todo.Id.ToString()); Assert.Null(element); }
[InlineData("Football", "", TodoStatus.InProgress, "2020-05-15T14:29:15.1823029Z", "2020-05-19T21:00:00.0000000Z")] // without Description public void GivenValidTodo_Insert_ReturnsNewTodo(string title, string description, TodoStatus status, string createdOn, string dueDate) { //arrange var repo = new Xml.TodoRepository(MockXmlContext.Object); var entity = new TodoModel() { Title = title, Description = description, Status = status, CreatedOn = DateTime.Parse(createdOn, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind), DueDate = DateTime.Parse(dueDate, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind) }; //act var todo = repo.Insert(entity); //assert Assert.NotNull(todo); Assert.NotEmpty(todo.Id.ToString()); Assert.NotEqual("00000000-0000-0000-0000-000000000000", todo.Id.ToString()); Assert.Equal(todo.Title, entity.Title); Assert.Equal(todo.Description, entity.Description); Assert.Equal(todo.Status, entity.Status); Assert.Equal(todo.CreatedOn, entity.CreatedOn); Assert.Equal(todo.DueDate, entity.DueDate); }
public async Task <ActionResult <TodoModel> > PostTodoModel(TodoModel todoModel) { _context.TodoModel.Add(todoModel); await _context.SaveChangesAsync(); return(CreatedAtAction("GetTodoModel", new { id = todoModel.ID }, todoModel)); }
[InlineData("a00e0400-3000-0000-3000-000050000001", "Football", "", TodoStatus.InProgress, "2020-05-15T14:29:15.1823029Z", "2020-05-19T21:00:00.0000000Z")] // without Description public void GivenValidEntity_Update_UpdateEntity(string id, string title, string description, TodoStatus status, string createdOn, string dueDate) { //arrange var todo = new TodoModel() { Id = Guid.Parse(id), Title = title, Description = description, Status = status, CreatedOn = DateTime.Parse(createdOn), DueDate = DateTime.Parse(dueDate) }; var todoAsElement = EntityToElement(todo); Container.Add(todoAsElement); var repo = new Xml.TodoRepository(MockXmlContext.Object); //act todo.Title = "Concert"; todo.Description = "Go to Metallica concert"; repo.Update(todo); //assert var all = Container.Elements(); var element = all.First(a => a.Attribute("Id").Value == todo.Id.ToString()); Assert.Equal(todo.Id.ToString(), element.Attribute("Id").Value); Assert.Equal(todo.Title, element.Element("Title").Value); Assert.Equal(todo.Description, element.Element("Description").Value); Assert.Equal(todo.Status.ToString(), element.Element("Status").Value); Assert.Equal(todo.CreatedOn.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture), element.Element("CreatedOn").Value); Assert.Equal(todo.DueDate.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture), element.Element("DueDate").Value); }
// DELETE api/values public BaseResponse <string> Delete(TodoModel todoModel) { var response = new BaseResponse <string>(); //response = _iTodoBusinesslogic.Delete(todoModel); string connectString = ConfigurationManager.ConnectionStrings[connectstring].ConnectionString; SqlConnection myConnection = new SqlConnection(); myConnection.ConnectionString = connectString; SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "Update Todo set DeleteDate = @DeleteDate Where Id = " + todoModel.Id; sqlCmd.Connection = myConnection; var deleteDate = DateTime.Now; sqlCmd.Parameters.AddWithValue("@DeleteDate", deleteDate); myConnection.Open(); int rowInserted = sqlCmd.ExecuteNonQuery(); myConnection.Close(); if (rowInserted > 0) { response.Result = true; response.Message = "Delete Success!"; } return(response); }
// Post api/todos public BaseResponse <string> Post(TodoModel todoModel) { var response = new BaseResponse <string>(); //response = _iTodoBusinesslogic.Create(todoModel); string connectString = ConfigurationManager.ConnectionStrings[connectstring].ConnectionString; SqlConnection myConnection = new SqlConnection(); myConnection.ConnectionString = connectString; SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "INSERT INTO Todo (Name,Description,Ordering,CreateDate) Values (@Name,@Description,@Ordering,@CreateDate)"; sqlCmd.Connection = myConnection; var createDate = DateTime.Now; sqlCmd.Parameters.AddWithValue("@Name", todoModel.Name); sqlCmd.Parameters.AddWithValue("@Description", todoModel.Description); sqlCmd.Parameters.AddWithValue("@Ordering", todoModel.Ordering); sqlCmd.Parameters.AddWithValue("@CreateDate", createDate); myConnection.Open(); int rowInserted = sqlCmd.ExecuteNonQuery(); myConnection.Close(); if (rowInserted > 0) { response.Result = true; response.Message = "Insert Success!"; } return(response); }
public IActionResult Create([FromBody] TodoModel model) { // map model to entity var todo = _mapper.Map <Todo>(model); try { if (model.Tags != null && model.Tags.Any()) { foreach (var tag in model.Tags) { todo.TodoTags.Add(new TodoTag { Todo = todo, Tag = tag }); } } // create user todo.CreatedDate = DateTime.Now; todo.CreatedBy = Convert.ToInt32(User.Identity.Name); var result = _todoService.Create(todo); return(Ok(_mapper.Map <TodoModel>(result))); } catch (AppException ex) { // return error message if there was an exception return(BadRequest(new { message = ex.Message })); } }
public IHttpActionResult Post(TodoModel value) { if (value == null) { return(BadRequest("Product cannot be null")); } using (TodoEntities1 entities = new TodoEntities1()) { TodoList a = new TodoList(); a.Description = value.Description; a.isDeleted = false; a.Name = value.Name; a.StatusId = 1; if (!ModelState.IsValid) { return(BadRequest(ModelState)); } entities.TodoLists.Add(a); int i = entities.SaveChanges(); if (i == 0) { return(Conflict()); } return(Created <TodoList>(Request.RequestUri + a.Id.ToString(), a)); } }
[InlineData("Football", "", TodoStatus.InProgress, "2020-05-15T14:29:15.1823029Z", "2020-05-19T21:00:00.0000000Z")] // without Description public void GivenValidTodo_Insert_AddsTodo(string title, string description, TodoStatus status, string createdOn, string dueDate) { var repo = new Xml.TodoRepository(MockXmlContext.Object); var entity = new TodoModel() { Title = title, Description = description, Status = status, CreatedOn = DateTime.Parse(createdOn), DueDate = DateTime.Parse(dueDate) }; var todo = repo.Insert(entity); var all = Container.Elements(); var element = all.First(a => a.Attribute("Id").Value == todo.Id.ToString()); Assert.NotNull(element); Assert.Equal(todo.Id.ToString(), element.Attribute("Id").Value); Assert.Equal(todo.Title, element.Element("Title").Value); Assert.Equal(todo.Description, element.Element("Description").Value); Assert.Equal(todo.Status.ToString(), element.Element("Status").Value); Assert.Equal(todo.CreatedOn.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture), element.Element("CreatedOn").Value); Assert.Equal(todo.DueDate.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture), element.Element("DueDate").Value); }
public IHttpActionResult Put(int id, [FromBody] TodoModel value) { if (value == null) { return(BadRequest("Product cannot be null")); } using (TodoEntities1 entities = new TodoEntities1()) { TodoList a = entities.TodoLists.Where(item => item.Id == id).FirstOrDefault(); if (a == null) { return(BadRequest("Product not Found")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } a.Description = value.Description; a.isDeleted = value.isDeleted; a.Name = value.Name; a.StatusId = value.StatusId; int i = entities.SaveChanges(); return(Ok()); } }
public void GivenValidID_GetById_ReturnsCorrectTodo() { //arrange var expectedTodo = new TodoModel(); var guid = Guid.Parse("00000000-0000-0000-0000-000000000001"); expectedTodo.Id = guid; expectedTodo.Title = "Unit tests"; expectedTodo.Description = "Learn how to make unit tests"; expectedTodo.Status = TodoStatus.Open; expectedTodo.CreatedOn = DateTime.Parse("2020-04-15T14:29:15.1823029Z", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind); expectedTodo.DueDate = DateTime.Parse("2020-04-19T21:00:00.0000000Z", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind); var repo = new Xml.TodoRepository(MockXmlContext.Object); //act var todo = repo.GetById(guid); //assert Assert.Equal(todo.Id, expectedTodo.Id); Assert.Equal(todo.Title, expectedTodo.Title); Assert.Equal(todo.Description, expectedTodo.Description); Assert.Equal(todo.Status, expectedTodo.Status); Assert.Equal(todo.CreatedOn, expectedTodo.CreatedOn); Assert.Equal(todo.DueDate, expectedTodo.DueDate); }
public async Task <IActionResult> PutTodoModel(long id, TodoModel todoModel) { if (id != todoModel.Id) { return(BadRequest()); } _context.Entry(todoModel).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TodoModelExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public void UpdateTodo(TodoModel todoModel, out string errorMessage) { errorMessage = null; var user = _userService.GetCachedUser(); if (user is null) { errorMessage = "User is not authorized."; return; } var todoToUpdate = _context.TodoItem.FirstOrDefault (todo => todo.Id == todoModel.Id && todo.User.Id == user.Id); if (todoToUpdate is null) { errorMessage = "Todo not found."; return; } if (todoModel.Name != null) { todoToUpdate.Name = todoModel.Name; } if (todoModel.ChangeIsDone) { todoToUpdate.IsDone = !todoToUpdate.IsDone; } _context.SaveChanges(); }
/// <summary> /// Get all record /// </summary> /// <param name="connectString"></param> /// <returns></returns> public List<TodoModel> GetAll(string connectString) { SqlDataReader reader = null; SqlConnection myConnection = new SqlConnection(); myConnection.ConnectionString = connectString; SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "Select * from Todo where DeleteDate IS NULL"; sqlCmd.Connection = myConnection; myConnection.Open(); reader = sqlCmd.ExecuteReader(); TodoModel md = null; var listModel = new List<TodoModel>(); while (reader.Read()) { md = new TodoModel(); md.Id = Convert.ToInt32(reader.GetValue(0)); md.Name = reader.GetValue(1).ToString(); md.Description = reader.GetValue(2).ToString(); if (!string.IsNullOrEmpty(reader.GetValue(5).ToString())) { md.Ordering = Convert.ToInt32(reader.GetValue(5)); } listModel.Add(md); } return listModel; }
public TodoModel SetStateToDeleted(int todoId, bool state) { TodoModel todo = _todos.Where(x => x.TodoId == todoId).FirstOrDefault(); todo.State = state; return(todo); }
public void GetAll_ReturnsAllTodos() { var mockTodos = new TodoModel[] { new TodoModel() { Id = new Guid(), Title = "Test", Description = "Test Description", Status = TodoStatus.Open, DueDate = new DateTime(2021, 3, 4, 12, 30, 00), CreatedOn = new DateTime(2020, 11, 12, 11, 55, 13), }, new TodoModel() { Id = new Guid(), Title = "Second", Description = "Second Test", Status = TodoStatus.InProgress, DueDate = new DateTime(2021, 1, 24, 1, 22, 33), CreatedOn = new DateTime(2020, 10, 11, 12, 13, 14), }, }; MockRepository.Setup(a => a.GetAll()).Returns(mockTodos); var service = new Service.TodoService(MockRepository.Object); var todos = service.GetAll(); Assert.Equal(mockTodos, todos); }
public async Task DeleteRemovesInsertedTodo() { var todo = new TodoModel { Name = "the Todo" }; var createResponse = await client.PostAsync( "/api/todos", new StringContent(JsonConvert.SerializeObject(todo), Encoding.UTF8, "application/json") ); Assert.True(createResponse.IsSuccessStatusCode); var createdTodo = JsonConvert.DeserializeObject <TodoModel>( await createResponse.Content.ReadAsStringAsync()); var deleteResponse = await client.DeleteAsync( $"/api/todos/{createdTodo.Id}"); var getResponse = await client.GetAsync("/api/todos"); var todosList = JsonConvert.DeserializeObject <List <TodoModel> >( await getResponse.Content.ReadAsStringAsync()); Assert.False(todosList.Any(t => t.Id == createdTodo.Id && t.Name == createdTodo.Name)); }
public void GivenFilledRepository_HasTodos_ReturnTrue() { var mockTodos = new TodoModel[] { new TodoModel() { Id = Guid.Parse("0a000300-0600-0000-0100-0000f0700001"), Title = "Test", Description = "Test Description", Status = TodoStatus.Open, DueDate = new DateTime(2021, 3, 4, 12, 30, 00), CreatedOn = new DateTime(2020, 11, 12, 11, 55, 13), }, new TodoModel() { Id = Guid.Parse("0a000300-0600-0020-0100-0000f0300002"), Title = "Second", Description = "Second Test", Status = TodoStatus.InProgress, DueDate = new DateTime(2021, 1, 24, 1, 22, 33), CreatedOn = new DateTime(2020, 10, 11, 12, 13, 14), }, }; MockRepository.Setup(a => a.GetAll()).Returns(mockTodos); var service = new Service.TodoService(MockRepository.Object); var hasTodos = service.HasTodos(); Assert.True(hasTodos); }
private void AddTodo() { if (!string.IsNullOrEmpty(TaskName)) { using (UnitOfWork uow = new UnitOfWork()) { if (!uow.GetRepository <TodoModel>().Any(x => x.TaskName.Equals(TaskName))) { var todoModel = new TodoModel() { TaskName = TaskName, }; uow.GetRepository <TodoModel>().Add(todoModel); bool result = Convert.ToBoolean(uow.SaveChanges()); if (result) { TodoList.Add(todoModel); } MessageBox.Show("Todo created successfully.", "Success", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("This todo name has already taken. Select different one.", "Todo name has taken", System.Windows.MessageBoxButton.OK, MessageBoxImage.Information); } } } else { MessageBox.Show("Please fill in fields."); } }
public List <TodoItem> GetTodoItems() { using (TodoModel context = new TodoModel()) { return(context.TodoItems.ToList()); } }
public async Task <IActionResult> Create(TodoModel todo) { context.Todo.Add(todo); await context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetById), new { id = todo.Id }, todo)); }
protected void Click(MouseEventArgs e) { if (myTodoText == null) { Toaster.Add("Cannot add empty values", MatToastType.Warning, "Todo List", null); return; } if (editedID == 0) { var myTodoItem = new TodoModel() { Id = TodoList.Count() + 1, Deadline = myDeadline == null?DateTime.Now.AddDays(1) : ((DateTime)myDeadline), Todo = myTodoText, IsCompleted = false }; TodoList.Add(myTodoItem); myTodoText = null; myDeadline = null; Toaster.Add("New todo added.", MatToastType.Info, "Todo List", null); } else { var myTodo = TodoList.FirstOrDefault(x => x.Id == editedID); myTodo.Todo = myTodoText; myTodo.Deadline = myDeadline == null?DateTime.Now.AddDays(1) : ((DateTime)myDeadline); myTodoText = null; myDeadline = null; Toaster.Add("Todo edit finished.", MatToastType.Info, "Todo List", null); editedID = 0; } }
public static void SaveTodo(TodoModel Todo) { using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString())) { cnn.Execute("insert into Todo(Tag, Color, Priority, TodoText, IsChecked) values(@Tag, @Color, @Priority, @TodoText, @isChecked)", Todo); } }