public void Unsubscribe(int userId, int establishmentId) { var user = _context.User.Include(u => u.Subscriptions).SingleOrDefault(u => userId == u.UserId); var establishment = _context.Establishment.Find(establishmentId); if (user == null) { throw new AuthenticationException("User does not exist"); } if (establishment == null) { throw new AuthenticationException("Establishment does not exist"); } var userEstablishment = user.Subscriptions.FirstOrDefault(ue => ue.UserId == userId && ue.EstablishmentId == establishmentId); // check if user is subscribed if (userEstablishment == null) { throw new AuthenticationException("User is not subscribed to this establishment"); } user.Subscriptions.Remove(userEstablishment); _context.Update(user); _context.SaveChanges(); }
public Establishment Update(Establishment establishmentParam) { var establishment = _context.Establishment.Include(e => e.Address) .Single(e => e.EstablishmentId == establishmentParam.EstablishmentId); establishment.Address.Street = establishmentParam.Address.Street; establishment.Address.Number = establishmentParam.Address.Number; _context.Update(establishment); _context.SaveChanges(); return(establishment); }
public Store Create(Store store, int categoryId, string image, string fileName, int userId) { var category = _context.Category.Find(categoryId); store.Category = category ?? throw new StoreException($"The category with id {categoryId} does not exist"); if (_context.Store.Any(s => s.Name == store.Name)) { throw new StoreException($"The store with name {store.Name} already exists"); } _context.Store.Add(store); _context.SaveChanges(); var newFileName = $"{store.StoreId}{Path.GetExtension(fileName)}"; var imgPath = $"/img/{newFileName}"; var relPath = $"wwwroot/img/{newFileName}"; File.WriteAllBytes(relPath, Convert.FromBase64String(image)); store.ImgPath = imgPath; _context.Store.Update(store); var user = _context.User.Find(userId); if (user == null) { throw new StoreException($"User with id {userId} does not exist"); } user.StoreId = store.StoreId; _context.Update(user); _context.SaveChanges(); // stop tracking store to avoid persisting absolute path var entry = _context.ChangeTracker.Entries().Single(e => e.Entity == store); entry.State = EntityState.Detached; SetImgPathHostName(store); return(store); }