示例#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());
        }
示例#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);
        }
示例#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));
        }
示例#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);
            }
        }
示例#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("/"));
        }
示例#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());
            }
        }
示例#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)));
        }
示例#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));
        }
示例#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"));
        }
示例#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));
        }
示例#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));
        }
示例#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));
        }
示例#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));
 }
示例#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());
        }
示例#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();
        }
示例#17
0
        public void Update(Todo todo)
        {
            var upTodo = _context.Todos.Find(todo.Id);

            upTodo.Title = todo.Title;
            _context.Update(upTodo);
            Save();
        }
示例#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());
        }
示例#19
0
        public async Task <ActionResult <ToDoItem> > Update(ToDoItem todoItem)
        {
            var newItem = await _todoContext.Update(todoItem);

            if (newItem == null)
            {
                return(BadRequest());
            }
            return(Ok(newItem));
        }
示例#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();
        }
示例#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));
        }
示例#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());
        }
        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);
        }
示例#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));
        }
示例#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}");
            }
        }
示例#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);
        }
示例#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());
        }
示例#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));
        }
示例#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));
        }
示例#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());
        }