예제 #1
0
        [HttpPost]//->>>>>>>UPDATE Method
        public async Task <IActionResult> Post([FromBody] Todo todo)
        {
            // if the model is bad then return bad calls
            if (todo is null || !ModelState.IsValid)
            {
                return(BadRequest("Nope"));
            }
            // if found of to do and the id has something
            if (todo.ListId.HasValue &&
                !(await _context.Todos.AnyAsync(l => l.Id == todo.ListId)))
            {
                return(BadRequest("nope"));
            }

            await _context.Todos.AddAsync(todo);

            try
            {
                //adding items to the saving of the changes
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(BadRequest("nope"));
            }

            return(CreatedAtAction("GetToDo", new { todo.Id }, todo));
        }
예제 #2
0
        public async Task <int> AddAsync(ToDoItem entity)
        {
            _toDoDbContext.ToDoItems.Add(entity);
            await _toDoDbContext.SaveChangesAsync();

            return(entity.Id);
        }
예제 #3
0
        public async Task <IActionResult> PutToDoItem(int id, ToDoItem toDoItem)
        {
            if (id != toDoItem.ToDoItemId)
            {
                return(BadRequest());
            }

            _context.Entry(toDoItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ToDoItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        [HttpPost] // ->>> UPDATE METHOD
        public async Task <IActionResult> Post([FromBody] TodoList list)
        {
            // if list is empty returns empty list
            if (list is null || !ModelState.IsValid)
            {
                return(BadRequest("Empty"));
            }

            // if there is items on the list better add to them
            await _context.TodoLists.AddAsync(list);

            try
            {
                //save items
                await _context.SaveChangesAsync();
            }
            catch
            {
                // TODO: Insert logging here
                return(BadRequest("nope"));
            }

            //find the new list items
            return(CreatedAtAction("GetToDoList", new { list.Id }, list));
        }
예제 #5
0
        public async Task <IActionResult> PutToDo([FromRoute] int id, [FromBody] ToDo toDo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != toDo.ToDoId)
            {
                return(BadRequest());
            }

            _context.Entry(toDo).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ToDoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #6
0
        public async Task <IActionResult> PutUser(int id, User user)
        {
            if (id != user.UserId)
            {
                return(BadRequest());
            }

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IHttpActionResult> PutToDoItem(int id, ToDoItem toDoItem)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != toDoItem.ToDoItemID)
            {
                return(BadRequest());
            }

            db.Entry(toDoItem).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ToDoItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
예제 #8
0
        public async Task <ToDo> Create(ToDo toDo)
        {
            _context.ToDos.Add(toDo);
            await _context.SaveChangesAsync();

            return(toDo);
        }
예제 #9
0
        public async Task AddItemAsync(ToDoItem item)
        {
            await _dbContext.ToDoItem.AddAsync(item);

            await _dbContext.SaveChangesAsync();

            _logger?.LogInformation($"{item.Name} created.");
        }
예제 #10
0
        public async Task <IActionResult> Post([FromBody] ToDo todo)
        {
            await _context.AddAsync(todo);

            await _context.SaveChangesAsync();

            return(CreatedAtAction("Get", todo));
        }
        public async Task <IActionResult> Create([FromBody] ToDoList toDoList)
        {
            await _context.ToDoLists.AddAsync(toDoList);

            await _context.SaveChangesAsync();

            return(CreatedAtRoute("GetList", new { id = toDoList.ID }, toDoList));
        }
예제 #12
0
        public async Task <T> AddToDoItem(T item)
        {
            await _context.Set <T>().AddAsync(item);

            await _context.SaveChangesAsync();

            return(item);
        }
예제 #13
0
        public async Task <IActionResult> Create(Courss cr)
        {
            _context.Courss.Add(cr);
            await _context.SaveChangesAsync();

            await Task.Factory.StartNew(() => { return(JsonConvert.SerializeObject(cr)); });

            return(RedirectToAction("Index"));
        }
예제 #14
0
        /// <summary>
        /// Adds ToDoItem record to ToDoItem table.
        /// </summary>
        /// <param name="createToDoItemDto"></param>
        /// <returns>added ToDoItem record.</returns>
        public async Task <ToDoItemDto> AddToDoItem(CreateToDoItemDto createToDoItemDto)
        {
            ToDoItemDbModel toDoItemDbDto = _mapper.Map <ToDoItemDbModel>(createToDoItemDto);

            toDoItemDbDto.CreationDate = DateTime.UtcNow;
            _toDoDbContext.ToDoItems.Add(toDoItemDbDto);
            await _toDoDbContext.SaveChangesAsync();

            return(_mapper.Map <ToDoItemDto>(toDoItemDbDto));
        }
        public async Task <IActionResult> Create([Bind("UserId,UserName")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
예제 #16
0
        public async Task <IActionResult> Create([Bind("ToDoId,ToDoItem")] ToDo toDo)
        {
            if (ModelState.IsValid)
            {
                _context.Add(toDo);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(toDo));
        }
예제 #17
0
        public async Task <IActionResult> Create([Bind("CategoryId,CategoryName")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
예제 #18
0
        public async Task <ActionResult> Create([Bind(Include = "ToDoItemID,Title,Status")] ToDoItem toDoItem)
        {
            if (ModelState.IsValid)
            {
                db.ToDoItems.Add(toDoItem);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(toDoItem));
        }
예제 #19
0
        public async Task CompleteTaskAsync(string?taskId)
        {
            var task = await context.FindAsync <TaskModel>(taskId);

            if (task == null)
            {
                throw new InvalidOperationException("Can not move task into Completed state. Task with given ID was not found.");
            }

            task.IsCompleted = true;
            await context.SaveChangesAsync();
        }
예제 #20
0
        public async Task <IActionResult> Create([FromBody] ToDoItem item)
        {
            if (item.ListID == 0)
            {
                item.ListID = 1;
            }
            await _context.ToDoItems.AddAsync(item);

            await _context.SaveChangesAsync();

            return(CreatedAtRoute("GetToDo", new { id = item.ID }, item));
        }
        public async Task <IActionResult> Create([Bind("ToDoId,ToDoName,UserId")] ToDo toDo)
        {
            if (ModelState.IsValid)
            {
                _context.Add(toDo);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["UserId"] = new SelectList(_context.Users, "UserId", "UserName", toDo.UserId);
            return(View(toDo));
        }
예제 #22
0
        public async Task <IActionResult> PostTodo([FromBody] string toDoName)
        {
            if (string.IsNullOrEmpty(toDoName))
            {
                return(BadRequest("Name should not be empty"));
            }

            var toDoItemModel = new ToDoItemModel(toDoName);
            await _dbContext.AddAsync(toDoItemModel);

            await _dbContext.SaveChangesAsync();

            return(Created($"todos/{toDoItemModel.Id}", toDoItemModel.Id));
        }
예제 #23
0
        public async Task <IActionResult> Create(DoList item)
        {
            if (ModelState.IsValid)
            {
                item.Date = DateTime.Now;
                context.Add(item);
                await context.SaveChangesAsync();

                TempData["Success"] = "The task has been added.";
                return(RedirectToAction("Index"));
            }

            return(View(item));
        }
예제 #24
0
        public async Task <IActionResult> PatchToDoItem(int id,
                                                        JsonPatchDocument <ToDoItem> patchDocument)
        {
            var item = await dbContext.ToDoItems.FindAsync(id);

            if (item == null)
            {
                return(NotFound());
            }

            patchDocument.ApplyTo(item);
            await dbContext.SaveChangesAsync();

            return(Ok());
        }
예제 #25
0
        public void TestGettingAllTodosInDb()
        {
            ToDoDbContext _context;
            DbContextOptions <ToDoDbContext> options = new DbContextOptionsBuilder <ToDoDbContext>()
                                                       .UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;

            using (_context = new ToDoDbContext(options))
            {
                List <ToDo> testTodos = GetTodosObjects();

                // Adding two objects into the context to check if both are stored in database
                foreach (ToDo x in testTodos)
                {
                    _context.ToDos.AddAsync(x);
                }
                _context.SaveChangesAsync();

                // Arrange
                TaskManagerController taskManagerController = new TaskManagerController(_context);

                // Act
                IEnumerable <ToDo> result     = taskManagerController.Get();
                List <ToDo>        resultList = result.ToList();

                // Assert
                Assert.Equal(testTodos.Count, resultList.Count);
            }
        }
        public async Task <IActionResult> Edit(int id, [Bind("HomeworkId,HomeworkDescription,DateLimit,difficulty,Label,idmodule")] Homework homwrk)
        {
            if (id != homwrk.HomeworkId)
            {
                return(RedirectToAction("Error"));
            }

            if (ModelState.IsValid)
            {
                _context.Update(homwrk);
                await _context.SaveChangesAsync();

                return(RedirectToAction("GetAll"));
            }
            return(View(homwrk));
        }
예제 #27
0
        /// <summary>
        /// Register user
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns> Success/Failure result</returns>
        public async Task <bool> RegisterUser(CreateUserDto userDto)
        {
            if (userDto.Password != null)
            {
                userDto.Password = CommonHelper.EncodePasswordToBase64(userDto.Password);
            }

            UserDbModel userName = await _toDoDbContext.Users
                                   .Where(p => p.UserName.ToLower() == userDto.UserName.ToLower()).FirstOrDefaultAsync();

            if (userName != null)           //This will prevent addition of existing username again
            {
                return(false);
            }
            UserDbModel user = _mapper.Map <UserDbModel>(userDto);

            if (user.UserRole == null)
            {
                user.UserRole = "User";
            }
            _toDoDbContext.Users.Add(user);
            if (await _toDoDbContext.SaveChangesAsync() == 1)
            {
                return(true);
            }
            return(false);
        }
예제 #28
0
        public async void ListNumber()
        {
            //database set up
            DbContextOptions <ToDoDbContext> options = new DbContextOptionsBuilder <ToDoDbContext>()
                                                       .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                                       .Options;

            using (ToDoDbContext context = new ToDoDbContext(options))
            {
                // Arrange
                await context.TodoLists.AddRangeAsync(
                    new TodoList()
                {
                    Name = "Get Milk",
                },

                    new TodoList()
                {
                    Name = "Throw Away Milk",
                }
                    );

                await context.SaveChangesAsync();

                To_ListController controller = new To_ListController(context);

                // Act
                OkObjectResult   result = controller.Getall() as OkObjectResult;
                DbSet <TodoList> lists  = result.Value as DbSet <TodoList>;

                // Assert. Counts how many list number do you have
                Assert.Equal(2, await lists.CountAsync());
            }
        }// End of List number
예제 #29
0
        public async Task <IActionResult> Post([FromBody] ToDoList toDoList)
        {
            await _context.ToDoList.AddAsync(toDoList);

            await _context.SaveChangesAsync();

            return(RedirectToAction("Get", new { id = toDoList.ID }));
        }
예제 #30
0
        public async Task <IdentityResult> CreateAsync(User user, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            _context.Add(user);
            var affectedRows = await _context.SaveChangesAsync(cancellationToken);

            return(affectedRows > 0
                ? IdentityResult.Success
                : IdentityResult.Failed(new IdentityError()
            {
                Description = $"Could not create user {user.Username}."
            }));
        }