public async Task <UsersFavoriteProducts> AddProductToFavorites(string username, int productId) { if (!context.Users.Any(x => x.UserName == username)) { throw new ObjectNotFoundException(); } if (!context.Products.Any(x => x.Id == productId)) { throw new ObjectNotFoundException(); } if (context.UsersFavoriteProducts.Any(x => x.Username == username && x.ProductId == productId)) { throw new FormatException(); } var usf = new UsersFavoriteProducts() { ProductId = productId, Username = username }; context.UsersFavoriteProducts.Add(usf); await context.SaveChangesAsync(); return(usf); }
private async Task <Product> CreateProduct(Product product) { if (!context.Users.Any(x => x.UserName == product.UserName)) { throw new ObjectNotFoundException(); } product.DatePublished = DateTime.Now; product.UserId = context.Users.Single(x => x.UserName == product.UserName).UserId; context.Cities.Attach(context.Cities.Single(x => x.Id == product.CityId)); context.Categories.Attach(context.Categories.Single(x => x.Id == product.CategoryId)); context.Products.Add(product); await context.SaveChangesAsync(); return(product); }
public async Task <Alert> AddAlert(Alert alert) { if (!alert.IsValid()) { throw new FormatException(); } if (!context.Users.Any(x => x.UserId == alert.UserId)) { throw new ObjectNotFoundException(); } var newAlert = context.Alerts.Add(alert); await context.SaveChangesAsync(); return(newAlert); }
public async Task AddImprovement(Improvement improvement) { if (!improvement.IsValid()) { throw new FormatException(); } context.Improvements.Add(improvement); await context.SaveChangesAsync(); }
private async Task <Message> CreateMessage(Message message) { if (!message.IsValid()) { throw new FormatException(); } if (!context.Users.Any(x => x.UserName == message.UserName)) { throw new ObjectNotFoundException(); } message.DateCreated = DateTime.Now; message.UserId = context.Users.Single(x => x.UserName == message.UserName).UserId; context.Messages.Add(message); await context.SaveChangesAsync(); return(message); }
private async Task <DonkeySellUser> UpdateUser(ViewUser viewUser) { var user = await userManager.FindAsync(viewUser.UserName, viewUser.Password); if (user == null) { throw new ObjectNotFoundException(); } user.Email = viewUser.Email; user.Address = viewUser.Address; user.Avatar = viewUser.Avatar; user.Facebook = viewUser.Facebook; user.Phone = viewUser.Phone; user.Twitter = viewUser.Twitter; await context.SaveChangesAsync(); return(user); }
public async Task <Friend> AddUserFriend(string username, string friend) { if (!context.Users.Any(x => x.UserName == username || x.UserName == friend)) { throw new ObjectNotFoundException(); } if (context.Friends.Any(x => x.Username == username && x.FriendUser == friend)) { throw new FormatException(); } var newFriend = context.Friends.Add(new Friend() { Username = username, FriendUser = friend }); await context.SaveChangesAsync(); return(newFriend); }