public Establishment Create(Establishment establishment, int storeId, string image, string fileName) { var store = _context.Store.Find(storeId); establishment.Store = store ?? throw new EstablishmentException($"The store with id {storeId} does not exist"); _context.Establishment.Add(establishment); _context.SaveChanges(); var newFileName = $"{establishment.EstablishmentId}{Path.GetExtension(fileName)}"; var imgPath = $"/img/establishments/{newFileName}"; var relPath = $"wwwroot/img/establishments/{newFileName}"; File.WriteAllBytes(relPath, Convert.FromBase64String(image)); establishment.ImgPath = imgPath; _context.Establishment.Update(establishment); _context.SaveChanges(); // stop tracking establishment to avoid persisting absolute path var entry = _context.ChangeTracker.Entries().Single(e => e.Entity == establishment); entry.State = EntityState.Detached; SetImgPathHostName(establishment); return(establishment); }
public void DeletePromotionForStore(int promotionId) { var promotion = _context.Promotion.Find(promotionId); if (promotion != null) { _context.Promotion.Remove(promotion); _context.SaveChanges(); } }
public Event Add(Event @event, int establishmentId) { var establishment = _context.Establishment.Find(establishmentId); if (establishment == null) { throw new EventException($"The establishment with id {establishmentId} does not exist"); } @event.Establishment = establishment; _context.Event.Add(@event); _context.SaveChanges(); return(@event); }
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); }
public User Create(User user, string password) { if (string.IsNullOrWhiteSpace(password)) { throw new AuthenticationException("Password is required"); } if (_context.User.Any(u => u.Username == user.Username)) { throw new AuthenticationException($"Username {user.Username} is already taken"); } CreatePasswordHash(password, out var passwordHash, out var passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; _context.User.Add(user); _context.SaveChanges(); return(user); }