public string GetMaxIds() { MaxIds maxIds = _db.MaxIds.FirstOrDefault(); if (maxIds == null) { maxIds = new MaxIds(); } return(JsonSerializer.Serialize <MaxIds>(maxIds)); }
// GET: Assignment4 public ActionResult Index() { MaxIds maxIds = new MaxIds(); maxIds.teacherMaxId = new TeacherDataController().getHighestId(); maxIds.studentMaxId = new StudentDataController().getHighestId(); maxIds.classMaxId = new ClassDataController().getHighestId(); ViewBag.Title = "Assignment 4"; return(View(maxIds)); }
public void AddCacheItem(CacheItem cacheItem) { Author locAuthor = _db.Authors.FirstOrDefault(el => el.Id == cacheItem.Author.Id); if (locAuthor == null) { _cache.Set($"Authros{cacheItem.Author.Id}", cacheItem.Author, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(OverridingCacheStorage) }); cacheItem.Author.Id = 0; _db.Authors.Add(cacheItem.Author); locAuthor = cacheItem.Author; } cacheItem.Book.Author = locAuthor; Category locCategory = _db.Categories.FirstOrDefault(el => el.Id == cacheItem.Category.Id); if (locCategory == null) { _cache.Set($"Categories{cacheItem.Category.Id}", cacheItem.Category, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(OverridingCacheStorage) }); cacheItem.Category.Id = 0; _db.Categories.Add(cacheItem.Category); locCategory = cacheItem.Category; } cacheItem.Book.Category = locCategory; Language locLanguage = _db.Languages.FirstOrDefault(el => el.Id == cacheItem.Language.Id); if (locLanguage == null) { _cache.Set($"Languages{cacheItem.Language.Id}", cacheItem.Language, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(OverridingCacheStorage) }); cacheItem.Language.Id = 0; _db.Languages.Add(cacheItem.Language); locLanguage = cacheItem.Language; } cacheItem.Book.Language = locLanguage; Publisher locPublisher = _db.Publishers.FirstOrDefault(el => el.Id == cacheItem.Publisher.Id); if (locPublisher == null) { _cache.Set($"Publishers{cacheItem.Publisher.Id}", cacheItem.Publisher, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(OverridingCacheStorage) }); cacheItem.Publisher.Id = 0; _db.Publishers.Add(cacheItem.Publisher); locPublisher = cacheItem.Publisher; } cacheItem.Book.Publisher = locPublisher; cacheItem.Book.Id = 0; _db.Books.Add(cacheItem.Book); _db.SaveChanges(); MaxIds maxIds = new MaxIds(); if (_db.Books.Count() > 0) { maxIds.MaxBookId = _db.Books.Max(el => el.Id); } if (_db.Categories.Count() > 0) { maxIds.MaxCategoryId = _db.Categories.Max(el => el.Id); } if (_db.Languages.Count() > 0) { maxIds.MaxLanguageId = _db.Languages.Max(el => el.Id); } if (_db.Publishers.Count() > 0) { maxIds.MaxPublisherId = _db.Publishers.Max(el => el.Id); } if (_db.Authors.Count() > 0) { maxIds.MaxAuthorId = _db.Authors.Max(el => el.Id); } _db.MaxIds.RemoveRange(_db.MaxIds.ToList()); _db.MaxIds.Add(maxIds); int n = _db.SaveChanges(); if (n > 0) { _cache.Set($"Books{cacheItem.Book.Id}", cacheItem.Book, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(OverridingCacheStorage) }); } }