Esempio n. 1
0
        public async Task <ActionResult <ApiMessage <MediaTitleVm> > > Get(int id)
        {
            var cacheKey = CacheKeys.MediaTitle(id);

            if (_cache.TryGetValue(cacheKey, out var cachedEntity))
            {
                _logger.LogDebug("Retrieved from cache!");
                return(ApiMessage.From(_mapper.Map <MediaTitleVm>(cachedEntity as MediaTitle)));
            }

            var entity = await _service.Get(id);

            if (entity == null)
            {
                return(NotFound(new ApiMessage {
                    Error = ApiError.NotFound
                }));
            }

            var cacheEntryOptions = new MemoryCacheEntryOptions()
                                    .SetSlidingExpiration(TimeSpan.FromSeconds(5))
                                    .SetSize(1);

            _cache.Set(cacheKey, entity, cacheEntryOptions);

            return(ApiMessage.From(_mapper.Map <MediaTitleVm>(entity)));
        }
Esempio n. 2
0
        public async Task <ActionResult <ApiMessage <MediaTitleVm> > > Put(int id, [FromBody] MediaTitleVm title)
        {
            // find first to determine type
            var entity = await _service.Get(id);

            if (entity == null)
            {
                return(NotFound(new ApiMessage {
                    Error = ApiError.NotFound
                }));
            }

            title.Id = id;
            if (entity.Type == MediaTitleType.Movie)
            {
                var movie = _mapper.Map <Movie>(title);
                await _service.Update(movie);
            }
            else if (entity.Type == MediaTitleType.Series)
            {
                var series = _mapper.Map <Series>(title);
                await _service.Update(series);
            }
            // invalidate cache
            _cache.Remove(CacheKeys.MediaTitle(id));

            return(ApiMessage.From(title));
        }
Esempio n. 3
0
        public ActionResult <ApiMessage <string> > Authenticate([FromBody] LoginRequest req)
        {
            var user = Users.FirstOrDefault(u => u.Email == req.Email && u.Password == req.Password);

            if (user == null)
            {
                return(BadRequest(new ApiMessage {
                    Error = ApiError.InvalidCredentials.Localized(_localizer)
                }));
            }

            return(ApiMessage.From(_jwtFactory.Generate(user.Email, user.Roles)));
        }
Esempio n. 4
0
        public async Task <ActionResult <ApiMessage <MediaTitleVm> > > Delete(int id)
        {
            var entity = await _service.Delete(id);

            if (entity == null)
            {
                return(NotFound(new ApiMessage {
                    Error = ApiError.NotFound
                }));
            }

            // invalidate cache
            _cache.Remove(CacheKeys.MediaTitle(id));

            return(ApiMessage.From(_mapper.Map <MediaTitleVm>(entity)));
        }
Esempio n. 5
0
        public async Task <ActionResult <ApiMessage <MediaTitleVm> > > Post([FromBody] MediaTitleVm title)
        {
            if (title.Type == MediaTitleType.Movie)
            {
                var movie = _mapper.Map <Movie>(title);
                await _service.Add(movie);

                title.Id = movie.Id;
            }
            else if (title.Type == MediaTitleType.Series)
            {
                var series = _mapper.Map <Series>(title);
                await _service.Add(series);

                title.Id = series.Id;
            }
            return(ApiMessage.From(title));
        }