public async Task <IActionResult> PutUserInfo([FromBody] UserInfo userInfo) { var claims = HttpContext.User.Claims; var id = claims.FirstOrDefault(x => x.Type == "Id")?.Value; var currentUserInfo = await _context.UserInfo.FirstOrDefaultAsync(info => info.UserId == id); currentUserInfo.UserPhone = userInfo.UserPhone; currentUserInfo.UserSex = userInfo.UserSex; currentUserInfo.UserBirthday = userInfo.UserBirthday; _context.Entry(currentUserInfo).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserInfoExists(currentUserInfo.Id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutProduct(int id, Product product) { if (id != product.Id) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Cart> > AddCart(int productId) { var claims = HttpContext.User.Claims; var userId = claims.FirstOrDefault(x => x.Type == "Id")?.Value; var cart = await _context.Carts.FirstOrDefaultAsync(c => c.UserId == userId && c.ProductId == productId); if (cart != null) { return(Ok(new BaseResult { Success = false, message = "Already added product." })); ; } var newCart = new Cart { UserId = userId, ProductId = productId }; await _context.Carts.AddAsync(newCart); var count = await _context.SaveChangesAsync(); return(Ok(new BaseResult { Success = count == 1 })); }
public async Task <IActionResult> Register([FromBody] UserRegistrationDto user) { if (ModelState.IsValid) { // We can utilise the model var existingUser = await _userManager.FindByEmailAsync(user.Email); if (existingUser != null) { return(BadRequest(new RegistrationResponse() { Errors = new List <string>() { "Email already in use" }, Success = false })); } var newUser = new IdentityUser { Email = user.Email, UserName = user.UserName }; var isCreated = await _userManager.CreateAsync(newUser, user.Password); if (isCreated.Succeeded) { user.UserId = newUser.Id; _ecDbContext.UserInfo.Add(user); await _ecDbContext.SaveChangesAsync(); var jwtToken = GenerateJwtToken(newUser); return(Ok(new RegistrationResponse() { Success = true, Token = jwtToken })); } else { return(BadRequest(new RegistrationResponse() { Errors = isCreated.Errors.Select(x => x.Description).ToList(), Success = false })); } } return(BadRequest(new RegistrationResponse() { Errors = new List <string>() { "Invalid payload" }, Success = false })); }