public IActionResult Post([FromBody] JObject value) { Todo myTask = new Todo(); myTask.Desc = value["desc"].ToString(); myTask.Date = value["date"].ToString(); List <string> tags = value["tags"].ToObject <List <string> >(); List <Tag> tagList = new List <Tag>(); for (int i = 0; i < tags.Count; i++) { tagList.Add(new Tag() { Name = tags[i] }); } var userId = HttpContext.User.Claims.First().Value; myTask.Tags = tagList; myTask.OwnerId = userId; dbContext.Todo.Add(myTask); dbContext.SaveChanges(); return(new ObjectResult(myTask)); }
public ActionResult <item> Create([FromBody] item item) { _context.Add(item); _context.SaveChanges(); // return CreatedAtRoute("GetItem", new { id = item.id.ToString() }, item); return(item); }
public void AddTodo(Todoes theTodo) { theTodo.StartDate = DateTime.Now; theTodo.StatusDate = DateTime.Now; theTodo.Id = 0; db.Todoes.Add(theTodo); db.SaveChanges(); }
public IQueryable AddItem([FromBody] TodoItem item) { if (item.Text != null || item.Category > 0) { _context.Add(new TodoItem(item.Text, item.Category)); _context.SaveChanges(); } return(_context.TodoItems.Where(i => i.Category == item.Category)); }
public ActionResult Create([Bind(Include = "ID,Name,DueDate,Type,Hours,Difficulty")] TodoList todoList) { if (ModelState.IsValid) { db.Todos.Add(todoList); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(todoList)); }
public int PutTodoItem([FromBody] TodoItem todoItem) { try { _context.Entry(todoItem).State = EntityState.Modified; _context.SaveChanges(); return(1); } catch { throw; } }
public ActionResult PostTodo([FromBody] TodoItem item) { try { _context.TodoItems.Add(item); _context.SaveChanges(); return(Json(new { success = true, data = "Todo added successfully" })); } catch (Exception ex) { return(Json(new { success = true, errorMessage = ex.Message })); } }
protected override Task <AddCategoryResponse> ProcessCommand(AddCategoryCommand command) { var response = new AddCategoryResponse(); var request = command.Data; var user = _context.AcUsers.FirstOrDefault(e => e.UserName == request.UserName); var category = new AcCategory { CategoryName = request.Category, User = user, IsDeleted = false }; _context.AcCategories.Add(category); var result = _context.SaveChanges(); if (result > 0) { response.Category = _mapper.Map <Category>(category); } else { throw new Exception("An error occur while adding category."); } return(Task.FromResult(response)); }
protected override Task <UpdateCategoryResponse> ProcessCommand(UpdateCategoryCommand command) { var response = new UpdateCategoryResponse(); var request = command.Data; var user = _context.AcUsers.FirstOrDefault(e => e.UserName == request.UserName); var category = _context.AcCategories.FirstOrDefault(e => e.CategoryId == request.CategoryId); if (category != null) { category.CategoryName = request.Name; _context.Attach(category); var result = _context.SaveChanges(); if (result == 0) { throw new Exception("An error occured while updating category"); } else { response.Category = _mapper.Map <Category>(category); } } return(Task.FromResult(response)); }
public Dictionary <string, object> CompleteToDo(int id) { using (var Context = new TodoDBContext()) { if (Context.TodoList.Where(x => x.Id == id).Count() == 1) { var todo = Context.TodoList.Where(x => x.Id == id).FirstOrDefault(); todo.Done = !todo.Done; Context.SaveChanges(); return(new Dictionary <string, object>() { { "success", true }, { "message", "Todo updated successfully" } }); } else { return(new Dictionary <string, object>() { { "success", false }, { "message", "No todo exists with that Id" } }); } } }
public Dictionary <string, object> AddNewTodo(Todo todo) { int count = userContext.UserList.Where(x => x.Id == todo.user).Count(); if (todo.Name == null || todo.Name.Length == 0 || count <= 0) { return(new Dictionary <string, object>() { { "success", false }, { "message", "Please enter details correctly" } }); } bool result = false; using (var Context = new TodoDBContext()) { Context.TodoList.Add(todo); Context.SaveChanges(); result = true; } if (result) { return(new Dictionary <string, object>() { { "success", true } }); } return(new Dictionary <string, object>() { { "success", false }, { "message", "Failed to add todo" } }); }
protected override Task <UpdateTodoResponse> ProcessCommand(UpdateTodoCommand command) { var response = new UpdateTodoResponse(); var request = command.Data; var user = _context.AcUsers.FirstOrDefault(e => e.UserName == request.UserName); var task = _context.AcTasks.FirstOrDefault(e => e.TaskId == request.TaskId); if (task != null) { var category = _context.AcCategories.FirstOrDefault(e => e.UserId == user.UserId && e.CategoryId == request.CategoryId && e.IsDeleted == false); if (category != null) { task.Name = request.Name; task.Category = category; task.RootTaskId = request.RootTaskId == 0 ? null : request.RootTaskId; task.Status = (short)request.TaskStatus; task.TaskPriorityId = (short)request.TaskPriority; using (var scope = _context.Database.BeginTransaction()) { try { _context.AcTasks.Attach(task); var activity = new AcTaskActivity { Activity = "Task Updated", ActivityDate = DateTime.Now, Task = task }; _context.Add(activity); _context.SaveChanges(); response.Todo = _mapper.Map <Todo>(task); scope.Commit(); } catch (Exception ex) { scope.Rollback(); throw new Exception("An error occured while creating task."); } } } else { throw new Exception("An error occured while creating task."); } } else { throw new Exception("An error occured while creating task."); } return(Task.FromResult(response)); }
//public IEnumerable<Status> GetAllStauses() //{ // var statuses = ctx.Statuses.ToList(); // return statuses; //} public Task Add(Task task) { int statusId = 1; long taskID = 0; if ((task.StatusId <= 1)) { task.StatusId = statusId; } else { task.StatusId = task.StatusId; } task.CreatedDate = DateTime.Now; task.UpdatedDate = DateTime.Now; ctx.Tasks.Add(task); ctx.SaveChanges(); //ctx.SaveChanges(); //(taskID) = task.Id; return(task); }
public void AddTodoStep(int todoId, string description) { //I found out that creating a temporary db context is working better. using (TodoDBContext theDB = new TodoDBContext()) { TodoSteps aStep = new TodoSteps() { Date = DateTime.Now, Description = description, TodoId = todoId }; theDB.TodoSteps.Add(aStep); theDB.SaveChanges(); } }
public IActionResult Delete(int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = _context.TodoItems.FirstOrDefault(x => x.ID == id); if (result != null) { _context.TodoItems.Remove(result); _context.SaveChanges(); } return(NoContent()); }
protected override Task <RegisterUserResponse> ProcessCommand(RegisterUserCommand command) { var response = new RegisterUserResponse(); var entity = _mapper.Map <AcUser>(command.Data); entity.CreateDate = DateTime.Now; var user = _context.AcUsers.Where(e => e.UserName == entity.UserName); if (user.Count() > 0) { throw new Exception("UserName is already in use"); } var email = _context.AcUsers.Where(e => e.Email == entity.Email); if (email.Count() > 0) { throw new Exception("Email is already in use"); } var security = new SecurityHelper(); var salt = security.HashCreate(); var passwordHash = security.HashCreate(entity.Password, salt); entity.Password = passwordHash.Split('æ')[0]; entity.Salt = passwordHash.Split('æ')[1]; using (var scope = _context.Database.BeginTransaction()) { try { _context.AcUsers.Add(entity); _context.AcCategories.Add(new AcCategory { CategoryName = "Project", User = entity }); _context.SaveChanges(); scope.Commit(); } catch { scope.Rollback(); throw new Exception("An error occured while inserting user to table."); } } return(Task.FromResult(response)); }
public TodoController(TodoDBContext context) { this._context = context; if (_context.TodoItems.Count() == 0) { var todo = new TodoItem { Name = "Take medicine", TodoDescription = "Take the Valoate CR 500 medicine at night.", IsComplete = false }; _context.Add(todo); _context.SaveChanges(); } }
public bool AddATodo(TodoList todo, string userName) { try { Registration registeredUser = _todoDBContext.Registrations.Where(user => user.UserName == userName).FirstOrDefault(); todo.Registration = registeredUser; _todoDBContext.TodoLists.Add(todo); _todoDBContext.SaveChanges(); return(OPERATION_SUCCESSFUL); } catch (Exception ex) { Console.WriteLine(ex.Message); return(OPERATION_FAILURE); } }
protected override Task <DeleteCategoryResponse> ProcessCommand(DeleteCategoryCommand command) { var response = new DeleteCategoryResponse(); var entity = _context.AcCategories.FirstOrDefault(e => e.CategoryId == command.Data.CategoryId && e.User.UserName == command.Data.UserName); entity.IsDeleted = true; _context.AcCategories.Attach(entity); var result = _context.SaveChanges(); if (result == 0) { throw new System.Exception("An error occured while deleting task."); } return(Task.FromResult(response)); }
public Dictionary <string, object> UpdateTodo(Todo updatedTodo) { int UserExists = userContext.UserList.Where(x => x.Id == updatedTodo.user).Count(); int TodoExists = Context.TodoList.Where(x => x.Id == updatedTodo.Id).Count(); if (updatedTodo.Name == null || updatedTodo.Name.Length == 0 || UserExists <= 0 || TodoExists <= 0) { return(new Dictionary <string, object>() { { "success", false }, { "message", "Please enter details correctly" } }); } using (var Context = new TodoDBContext()) { if (Context.TodoList.Where(x => x.Id == updatedTodo.Id).Count() == 1) { var todo = Context.TodoList.Where(x => x.Id == updatedTodo.Id).FirstOrDefault(); todo.Name = updatedTodo.Name; Context.SaveChanges(); return(new Dictionary <string, object>() { { "success", true }, { "message", "Todo updated successfully" } }); } else { return(new Dictionary <string, object>() { { "success", false }, { "message", "No todo exists with that Id" } }); } } }
public static void EnsureCreated(TodoDBContext context) { if (context.Database.EnsureCreated()) { var user = new AcUser { Email = "*****@*****.**", FirstName = "Burak", LastName = "Portakal", Password = "******",//testtest UserName = "******", Salt = "tOoByYVHjUQ4Ue+SWZPmEQ==", CreateDate = DateTime.Now }; context.AcUsers.Add(user); context.AcTaskStatuses.Add(new AcTaskStatus { Status = "Todo" }); context.AcTaskStatuses.Add(new AcTaskStatus { Status = "InProgress" }); context.AcTaskStatuses.Add(new AcTaskStatus { Status = "Completed" }); context.AcTaskPriorities.Add(new AcTaskPriority { Priority = "P1" }); context.AcTaskPriorities.Add(new AcTaskPriority { Priority = "P2" }); context.AcTaskPriorities.Add(new AcTaskPriority { Priority = "P3" }); context.AcCategories.Add(new AcCategory { CategoryName = "Project", User = user }); context.AcTasks.Add(new AcTask { Name = "Test task", CategoryId = 1, Status = 1, TaskPriorityId = 1, User = user, IsDeleted = false, CreateDate = DateTime.Now }); context.AcTasks.Add(new AcTask { Name = "Test task 2", CategoryId = 1, Status = 1, TaskPriorityId = 1, User = user, IsDeleted = false, CreateDate = DateTime.Now }); context.SaveChanges(); } }