示例#1
0
        public async Task <ActionResult> Update(HomeServiceModel model)
        {
            var result = await _homeService.UpdateAsync(model);

            if (result.Failure)
            {
                return(BadRequest(result.Error));
            }

            return(Ok());
        }
示例#2
0
        // PT: CACHE (RESPONSE CACHING - RECOMMENDED TO BROWSER) (step 3):
        //[ResponseCache(Location = ResponseCacheLocation.Client, Duration = 60 /*sec*/ * 10 /*min*/)]
        public async Task <IActionResult> Index()
        {
            this.logger.LogCritical("Hello");

            HomeServiceModel homeServiceModel = await homeService.GetHomePageInformationPackage();

            HomeViewModel model = this.mapper.Map <HomeViewModel>(homeServiceModel);

            WeatherViewModel weather = this.mapper.Map <WeatherViewModel>(homeServiceModel.RootWeather);

            model.Weather = weather;

            // PT: CACHE (IN-MEMORY CACHING) (step 3)
            if (!this.memoryCache.TryGetValue <DateTime>("TimeNowInMemory", out var valueInMemory))
            {
                valueInMemory = DateTime.UtcNow;
                this.memoryCache.Set("TimeNowInMemory", valueInMemory, TimeSpan.FromSeconds(10));

                //// PT: Every time someone gets this cached value its expiration will expand with (0,10,0) minutes.
                //this.memoryCache.Set("TimeNow", value, new MemoryCacheEntryOptions
                //{
                //    SlidingExpiration = new TimeSpan(0, 10, 0)
                //});
            }
            model.TimeNowCachedInMemory = valueInMemory;

            // PT: CACHE (DISTRIBUTED CACHING - SQLSERVER) (step 4)
            var valueDistributed = await this.distributedCache.GetStringAsync("TimeNowDistributed");

            if (valueDistributed == null)
            {
                valueDistributed = DateTime.UtcNow.ToString();
                await this.distributedCache.SetStringAsync("TimeNowDistributed", valueDistributed, new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1)
                });
            }
            model.TimeNowCachedDistributed = DateTime.Parse(valueDistributed);

            // PT: SESSION / COOKIES (step 4)
            string id = this.HttpContext.Session.Id;

            this.HttpContext.Session.SetString("TestSession", "SessionValueOrWhatever");
            string valueSession = this.HttpContext.Session.GetString("TestSession");

            // PT: TEMP DATA
            this.TempData["TempDataTest"] = "This is just a test on TempData.";
            // Once read TempData is cleared.

            return(View(model));
        }
        public async Task <HomeServiceModel> GetHomePageInformationPackage()
        {
            HomeServiceModel model = new HomeServiceModel
            {
                News          = await Get3RandomNews(),
                RootWeather   = GetWeatherRoot(),
                CountClasses  = this.db.Classes.Count(),
                CountClubs    = this.db.Clubs.Count(),
                CountParents  = this.db.Parents.Count(),
                CountTeachers = this.db.Teachers.Count(),
                CountStudents = this.db.Students.Count(),
                CountSubjects = this.db.Subjects.Count()
            };

            return(model);
        }
示例#4
0
        public async Task <ActionResult> Create(HomeServiceModel model)
        {
            var id = await _homeService.CreateAsync(model);

            return(Created(nameof(Create), id));
        }