Exemplo n.º 1
0
        public IActionResult Put([FromBody] PasswordDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("密码长度必须在6-20位之间!"));
            }

            User user = GetCurrentUser();

            if (dto.NewPassword == dto.OldPassword || dto.NewPassword == user.Password)
            {
                return(BadRequest(new
                {
                    msg = "新密码不能和旧密码相同!"
                }));
            }

            if (user.Password != dto.OldPassword)
            {
                return(BadRequest(new
                {
                    msg = "原密码错误!"
                }));
            }
            user.Password = dto.NewPassword;
            _context.Update(user);
            _context.SaveChanges();
            return(Ok());
        }
Exemplo n.º 2
0
        public AuthenticateResponse Authenticate(AuthenticateRequest model, string ipAddress)
        {
            var account = _context.Accounts.SingleOrDefault(x => x.Email == model.Email);

            if (account == null || !account.IsVerified || !BC.Verify(model.Password, account.PasswordHash))
            {
                throw new AppException("Email or Password is incorrect.");
            }

            // generate  authenticate
            var jwtToken     = generateJwtToken(account);
            var refreshToken = generateRefreshToken(ipAddress);

            // save
            account.RefreshTokens.Add(refreshToken);
            _context.Update(account);
            _context.SaveChanges();

            var response = _mapper.Map <AuthenticateResponse>(account);

            response.JwtToken     = jwtToken;
            response.RefreshToken = refreshToken.Token;

            return(response);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Edit(string id, [Bind("Name")] Tag tag)
        {
            if (id != tag.Name)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(tag);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TagExists(tag.Name))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(tag));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Edit(long id, [Bind("Id,Name,IsComplete")] TodoItem todoItem)
        {
            if (id != todoItem.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(todoItem);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TodoItemExists(todoItem.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(todoItem));
        }
        public void WhenUpdatingTodoItemThenChangesAreThereOnReadback()
        {
            var options = new DbContextOptionsBuilder <TodoContext>()
                          .UseInMemoryDatabase(databaseName: "Update_todos_to_database")
                          .Options;

            using (var context = new TodoContext(options))
            {
                context.TodoItems.AddRange(todosToAdd.ToArray());
                context.SaveChanges();

                var foundItem = context.TodoItems.Where(r => r.TodoTitle == "Initialize in Startup").FirstOrDefault();
                Assert.IsTrue(foundItem != null);
                Console.WriteLine("Found:");
                TraceTodoItem(foundItem);

                foundItem.TodoTitle     = "Initialize in webapp Startup";
                foundItem.IsComplete    = true;
                foundItem.WhenCompleted = DateTime.UtcNow;
                context.Update(foundItem);
                context.SaveChanges();

                var changedItem = context.TodoItems.Where(r => r.TodoTitle == "Initialize in webapp Startup").FirstOrDefault();
                Assert.IsTrue(changedItem != null);
                Assert.IsTrue(changedItem.IsComplete);
                Console.WriteLine("Changed to:");
                TraceTodoItem(changedItem);
            }
        }
Exemplo n.º 6
0
        public async Task <IActionResult> ChangePassword(string newPassword, string confirmPassword)
        {
            var userId = HttpContext.Session.GetInt32("UserId");

            if (userId == null)
            {
                return(Redirect("/login/login"));
            }

            User user = await _dbContext.Users.FirstOrDefaultAsync(u => u.UserId == userId);

            if (user != null && user.UserId == userId)
            {
                if (!string.IsNullOrWhiteSpace(newPassword) && !string.IsNullOrWhiteSpace(confirmPassword))
                {
                    if (newPassword == confirmPassword)
                    {
                        user.Password = BC.HashPassword(confirmPassword);
                    }
                }

                _dbContext.Update(user);
                _dbContext.SaveChanges();
            }

            return(Redirect("/"));
        }
Exemplo n.º 7
0
        public IActionResult UpdateUser(long userId, string userJson)
        {
            User user = UserFromJson(userJson);

            if (user == null)
            {
                return(BadRequest());
            }
            using (var context = new TodoContext()) {
                User origUser = context.Users.Find(userId);
                if (origUser == null)
                {
                    return(NotFound());
                }
                origUser.Name = user.Name;
                if (user.DefaultProject != null)
                {
                    origUser.DefaultProject = context.Projects
                                              .Find(user.DefaultProject.Pid);
                }
                context.Update(origUser);
                context.SaveChanges();
                return(NoContent());
            }
        }
Exemplo n.º 8
0
        public ActionResult <DiaryUserDto> PutStudent(long id, Diary diary)
        {
            User user   = GetCurrentUser();
            var  result = _context.Diary.Include(q => q.User).SingleOrDefault(o => o.Id == id);

            if (result == null || result.User != user)
            {
                return(BadRequest(new
                {
                    msg = "没有权限操作!"
                }));
            }
            result.Title    = diary.Title;
            result.Content  = diary.Content;
            result.IsPublic = diary.IsPublic;
            _context.Update(result);
            _context.SaveChanges();
            //diary.Id = result.Id;
            //_context.Entry(diary).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            //_context.Entry(diary).Property(x => x.CreateTime).IsModified = false;
            //_context.Entry(diary).Property(x => x.UserId).IsModified = false;
            //try
            //{
            //    _context.SaveChanges();
            //}
            //catch (DbUpdateConcurrencyException)
            //{
            //    return NotFound();
            //}

            return(CreatedAtAction(nameof(Get), new { id = diary.Id }, ModelToDtoUtil.GetDiaryUserDto(result, user)));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,UserID,Task,TaskDate")] Todo todo)
        {
            if (id != todo.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(todo);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TodoExists(todo.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["UserID"] = new SelectList(_context.Users, "ID", "ID", todo.UserID);
            return(View(todo));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> Edit(int todoId, string title, string description, string isDone)
        {
            var userId = HttpContext.Session.GetInt32("UserId");

            if (userId == null)
            {
                return(Redirect("/login/login"));
            }

            Todo todo = await _dbContext.Todos.FirstOrDefaultAsync(x => x.TodoId == todoId);

            if (todo != null && todo.UserId == userId)
            {
                todo.IsDone = isDone == "true";

                if (!string.IsNullOrWhiteSpace(todo.Title))
                {
                    todo.Title = _provider.Protect(title);
                }
                if (!string.IsNullOrWhiteSpace(todo.Description))
                {
                    todo.Description = _provider.Protect(description);
                }

                _dbContext.Update(todo);
                _dbContext.SaveChanges();
            }

            return(Redirect("/Todo/todo"));
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,Name,Description,Goal,DueDate,Comments")] Todo todo)
        {
            if (id != todo.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(todo);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TodoExists(todo.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(todo));
        }
Exemplo n.º 12
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,DeadlineDate,Description")] Todo.Model.Models.Task task)
        {
            if (id != task.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(task);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    if (!Utils.TaskExists(task.Id, _context))
                    {
                        //RH:
                        //return Conflict();
                        //return NotFound();
                        return(Conflict(new { message = $"Record '{id}' was not found." }));
                    }
                    else
                    {
                        throw ex; //RH: Pocyztaj o różnicy między throw a throw ex
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(task));
        }
Exemplo n.º 13
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,IsCompleted,CreatedDate,ModifiedDate,CategoryId")] Todo todo)
        {
            if (id != todo.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(todo);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TodoExists(todo.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Name", todo.CategoryId);
            return(View(todo));
        }
Exemplo n.º 14
0
 public async Task <IActionResult> Edit([Bind("FirstName,LastName")] User user)
 {
     if (ModelState.IsValid)
     {
         // If the Todo doesn't belong to our user
         User curUser = GetCurrentUser().Result;
         if (curUser == null)
         {
             return(Unauthorized());
         }
         curUser.FirstName = user.FirstName;
         curUser.LastName  = user.LastName;
         try {
             _context.Update(curUser);
             await _context.SaveChangesAsync();
         } catch (DbUpdateConcurrencyException) {
             if (!UserExists(user.Id))
             {
                 return(NotFound());
             }
             else
             {
                 throw;
             }
         }
         return(RedirectToAction(nameof(Index)));
     }
     return(View(user));
 }
Exemplo n.º 15
0
        public async Task <IActionResult> Update(int id, [FromBody] Todo todo)
        {
            if (id != todo.Id)
            {
                return(NotFound());
            }
            todo.UpdatedAt = DateTime.Now;
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(todo);
                    await _context.SaveChangesAsync();

                    return(Ok(todo));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TodoExists(todo.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            return(BadRequest());
        }
Exemplo n.º 16
0
        public void FullUpdate(long id, string newTask, bool isUrgent, bool isDone)
        {
            Todo todo = GetById(id);

            if (newTask != null)
            {
                todo.Task = newTask;
            }

            todo.IsUrgent = isUrgent;

            todo.IsDone = isDone;

            context.Update(todo);

            context.SaveChanges();
        }
Exemplo n.º 17
0
        public void Update(Todo todo)
        {
            var upTodo = _context.Todos.Find(todo.Id);

            upTodo.Title = todo.Title;
            _context.Update(upTodo);
            Save();
        }
Exemplo n.º 18
0
        public IActionResult Update(string id, [FromBody] TodoItem item)
        {
            if (item == null || item.TodoItemID != id)
            {
                return(BadRequest());
            }
            TodoItem todo = _dbContext.TodoItems.SingleOrDefault(t => t.TodoItemID == id);

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

            _dbContext.Update(item);
            _dbContext.SaveChanges();
            return(new NoContentResult());
        }
Exemplo n.º 19
0
        public async Task <ActionResult <ToDoItem> > Update(ToDoItem todoItem)
        {
            var newItem = await _todoContext.Update(todoItem);

            if (newItem == null)
            {
                return(BadRequest());
            }
            return(Ok(newItem));
        }
Exemplo n.º 20
0
        public async Task UpdateTodoAsync(Todo todo)
        {
            var foundTodo = await _todoContext.Todos.FindAsync(todo.TodoID);

            foundTodo.Title       = todo.Title;
            foundTodo.IsCompleted = todo.IsCompleted;
            foundTodo.UserID      = todo.UserID;
            _todoContext.Update(foundTodo);
            await _todoContext.SaveChangesAsync();
        }
Exemplo n.º 21
0
        public async Task <ActionResult> Edit(TodoList item)
        {
            if (ModelState.IsValid)
            {
                context.Update(item);
                await context.SaveChangesAsync();

                TempData["Success"] = "The item has been updated!";
                return(RedirectToAction("Index"));
            }
            return(View(item));
        }
Exemplo n.º 22
0
        public async Task <IActionResult> Put(long id, TodoItem todoItem)
        {
            if (todoItem.Id != id)
            {
                return(BadRequest());
            }

            _context.Update(todoItem);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Exemplo n.º 23
0
        public async Task <bool> UpdateAsync(Todo todo, CancellationToken ct = default(CancellationToken))
        {
            if (!await TodoExistsAsync(todo.TodoID, ct))
            {
                return(false);
            }
            _context.Todos.Update(todo);

            _context.Update(todo);
            await _context.SaveChangesAsync(ct);

            return(true);
        }
Exemplo n.º 24
0
        public async Task <ActionResult <TodoItem> > Put(TodoItem item)
        {
            if (item == null)
            {
                return(BadRequest());
            }
            if (!db.TodoItems.Any(x => x.Id == item.Id))
            {
                return(NotFound());
            }

            db.Update(item);
            await db.SaveChangesAsync();

            return(Ok(item));
        }
Exemplo n.º 25
0
        public async Task <Todo> UpdateAsync(Todo todo)
        {
            try
            {
                Todo toUpdate = await ctx.Todos.FirstAsync(t => t.TodoId == todo.TodoId);

                toUpdate.IsCompleted = todo.IsCompleted;
                ctx.Update(toUpdate);
                await ctx.SaveChangesAsync();

                return(toUpdate);
            }
            catch (Exception e)
            {
                throw new Exception($"Did not find todo with id{todo.TodoId}");
            }
        }
Exemplo n.º 26
0
        public ActionResult <TodoItem> UpdateById(long id, TodoItem todoItem)
        {
            var item = _context.TodoItems.Find(id);

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

            item.Name       = todoItem.Name;
            item.IsComplete = todoItem.IsComplete;

            _context.Update(item);
            _context.SaveChanges();

            return(item);
        }
Exemplo n.º 27
0
        public async Task <IActionResult> PatchTodoItem(long id, TodoItem todoItem)
        {
            if (id != todoItem.Id)
            {
                return(BadRequest());
            }
            var olditem = await _context.TodoItems.FindAsync(id);

            if (olditem == null)
            {
                return(NotFound());
            }
            olditem.IsComplete = todoItem.IsComplete;
            _context.Update(olditem);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Exemplo n.º 28
0
        public async Task <TodoModel> Update(int id, TodoModel model)
        {
            var entity = await _context.Todos.FindAsync(id);

            if (entity == null)
            {
                return(null);
            }

            entity.DeadLine    = model.DeadLine;
            entity.Description = model.Description;
            entity.IsCompleted = model.IsCompleted;

            _context.Update(entity);
            await _context.SaveChangesAsync();

            return(await GetById(id));
        }
Exemplo n.º 29
0
        public async Task <IActionResult> Edit(Guid id, [Bind("Title,Description")] Todo todo, List <Guid> TodoLabels)
        {
            if (ModelState.IsValid)
            {
                // If the Todo doesn't belong to our user
                Todo curTodo = _context.Todo.First(t => t.Id == id && t.User == GetCurrentUser().Result);
                if (curTodo == null)
                {
                    return(Unauthorized());
                }
                curTodo.Description          = todo.Description;
                curTodo.Title                = todo.Title;
                curTodo.LastModificationDate = DateTime.Now;
                curTodo.TodoLabels           = new List <TodoLabel>();
                foreach (Guid labelId in TodoLabels)
                {
                    TodoLabel tdl = new TodoLabel();
                    tdl.Todo      = curTodo;
                    tdl.LabelGuid = labelId;
                    curTodo.TodoLabels.Add(tdl);
                }
                try{
                    _context.Update(curTodo);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException) {
                    if (!TodoExists(todo.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            TodoViewModel tvm = new TodoViewModel();

            tvm.Todo   = todo;
            tvm.Labels = _context.Label.ToList();
            return(View(tvm));
        }
Exemplo n.º 30
0
        public async Task <IActionResult> PatchTodoItem(long id, TodoItem item)
        {
            if (id != item.Id)
            {
                return(BadRequest());
            }
            var todoItem = await _context.TodoItems.FindAsync(id);

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

            // if(item.IsComplete !=null wanted to use boolean here)
            todoItem.IsComplete = item.IsComplete;
            _context.Update(todoItem);
            await _context.SaveChangesAsync();

            return(NoContent());
        }