public IActionResult AddElement([FromBody] ElementCreationData elementData)
        {
            var userId     = _userProvider.GetCurrentUserId();
            var collection = _dbContext.Collection
                             .Include(c => c.Elements)
                             .SingleOrDefault(c => c.Id == elementData.CollectionId);

            if (collection == null)
            {
                return(NotFound());
            }

            if (collection.Elements.Count >= _configurationProvider.MaxElementsInCollection)
            {
                return(BadRequest("Could not add the element: Maximum number of elements reached"));
            }

            var lastSequenceInCollection = collection.Elements.Max(e => e.Sequence);
            var element = new Element
            {
                Link         = elementData.Link,
                Name         = elementData.Name,
                CollectionId = elementData.CollectionId,
                OwnerId      = userId,
                Sequence     = lastSequenceInCollection + 1 ?? 1
            };

            _dbContext.Add(element);
            _dbContext.SaveChanges();
            return(Ok());
        }
        public async Task <UserDTO> GetCurrentUser()
        {
            var user = await _userManager.FindByIdAsync(_userContextProvider.GetCurrentUserId());

            var roles = await _userManager.GetRolesAsync(user);

            return(UserDtoBuilder
                   .FromApplicationUser(user)
                   .WithRoles(roles)
                   .Create());
        }
        public ICollection <Collection> GetUserCollections()
        {
            var userId      = _userProvider.GetCurrentUserId();
            var collections = _dbContext.Users
                              .Include(u => u.Collections)
                              .ThenInclude(c => c.Elements)
                              .Single(u => u.Id == userId)
                              .Collections.ToList();

            return(collections);
        }
Beispiel #4
0
        public ICollection <SharedCollection> GetUserSharedCollections()
        {
            var userId            = _userProvider.GetCurrentUserId();
            var sharedCollections = _dbContext.Users
                                    .Include(u => u.SharedCollections)
                                    .ThenInclude(sc => sc.Collection)
                                    .ThenInclude(c => c.Elements)
                                    .Single(u => u.Id == userId)
                                    .SharedCollections.ToList();

            return(sharedCollections);
        }
Beispiel #5
0
        public ActionResult <ICollection <PublicCollectionVisit> > GetCollectionVisitorData(int id)
        {
            var userId     = _userContextProvider.GetCurrentUserId();
            var collection = _dbContext.Collection.SingleOrDefault(c => c.Id == id);

            if (collection == null)
            {
                return(NotFound());
            }

            if (collection.IsPublic == false)
            {
                return(BadRequest());
            }

            if (userId != collection.OwnerId)
            {
                return(Forbid());
            }

            var data = _cache.GetOrCreate(id, entry =>
            {
                entry.AbsoluteExpiration = DateTimeOffset.Now.AddDays(1);
                return(_dbContext.PublicCollectionVisit.Where(c => c.CollectionId == id).ToList());
            });

            return(data);
        }