public async Task <ActionResult <UserDto> > Register(RegisterDto registerDto) { if (CheckEmailExistsAsync(registerDto.Email).Result.Value) { return(new BadRequestObjectResult(new ApiResponse((int)HttpStatusCode.BadRequest, "Email address is in use"))); } var user = _mapper.Map <User>(registerDto); await _toDoContext.Users.AddAsync(user); var output = await _toDoContext.SaveChangesAsync(); if (output > 0) { var userDto = _mapper.Map <UserDto>(user); userDto.Token = _tokenService.CreateToken(user); return(Ok(userDto)); } return(BadRequest(new ApiResponse((int)HttpStatusCode.BadRequest))); }
// uppdatera public static async Task UpdateToDoAsync(int id) //nån form identifiering { using ToDoContext context = new ToDoContext(); var todo = await context.ToDos.FindAsync(id); /* när vi gör context.ToDos.FindAsync(id)--kör en tracking * den track så den vet att atribyt "context.ToDos.FindAsync(id)" har koopling till "var todo * den vet att den object vi har*/ if (todo != null) { todo.Completed = true; /* här tala jag om då --fören context del jag har ändrat på den objected*/ context.Entry(todo).State = EntityState.Modified; //Spara om --det som ändrat på object await context.SaveChangesAsync(); } //+ in programm }
public static async Task SaveNewList() { try { var addToDoList = new ToDoListModel { ListName = CreateNewListViewModel.ListName, LastUpdate = DateTime.Now }; await using var toDoContext = new ToDoContext(); toDoContext.Add(addToDoList); await toDoContext.SaveChangesAsync(); PostToDoList(toDoContext); } catch (Exception ex) { //Handle exception - Typically I use a service like rollbar Debug.WriteLine(ex); } }
public static async Task AddToDoAsync(string activity) { //instance //var context = new ToDoContext(); /*problem med den variant, som vi vanlig gör i Class, * om jag inte implementera IDispose, Task ta inte bort */ using ToDoContext context = new ToDoContext();// 1. koopla till DB-oppna och stänga dooren context.ToDos.Add(new ToDo(activity)); //2. förberreda vad jag ska göra //context.ToDos.Add(new ToDo { Activity = activity });// object variant om har inga CTOR //context.ToDos.Add(new ToDo { Activity = "" }); // kan skriva egna activity i "" // hittad från --public DbSet<ToDo> ToDos { get; set;} //i {} kan hitta Created från ToDo.cs || ()kan skapa ctor där och använda andra await context.SaveChangesAsync(); //3.utföra handling och gå genom door //de 3 raden --skapa nånting i database }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } if (Item.From > Item.To) { ModelState.AddModelError("Item.To", "End date should be greater than start date"); return(Page()); } if (Item.Id == 0) { todos.ToDo.Add(Item); } else { //TODO: better to fetch and then update todos.Update(Item); } await todos.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task CompleteAsync() { await _context.SaveChangesAsync(); }
public async Task UpdateAsync(Author author) { _dbContext.Entry(author).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); }
public async Task AddToDoAsync(ToDo todo) { await _context.ToDos.AddAsync(todo); await _context.SaveChangesAsync(); }
public async Task <int> Add(ToDoItem toDo) { await _context.ToDos.AddAsync(toDo); return(await _context.SaveChangesAsync()); }
public async Task Post(TodoList todoList) { await _context.ToDoList.AddAsync(todoList); await _context.SaveChangesAsync(); }
public async Task AddListAsync(IEnumerable <ToDoEntity> ToDos) { _ToDoContext.ToDos.AddRange(ToDos); await _ToDoContext.SaveChangesAsync(); }
public async Task AddToDoAsync(ToDo model) { context.ToDo.Add(model); await context.SaveChangesAsync(); }
public async Task <bool> SaveAll() { return(await _context.SaveChangesAsync() > 0); }