Ejemplo n.º 1
0
        public async Task <Response> Init(InitThreadDto initThreadDto)
        {
            //check if this thread already exists
            var existingThread = await GetThreadWithEverything(t =>
                                                               t.Namespace.NamespaceId == initThreadDto.NamespaceId && t.Identifier == initThreadDto.Identifier);

            if (existingThread != null)
            {
                existingThread.Views++;
                await _context.SaveChangesAsync();

                var existingThreadDto = _mapper.Map <ThreadDto>(existingThread);
                existingThreadDto.IsAdmin = _userService.IsAuthenticated ? existingThread.Namespace.User.Id == _userService.GetUserId() : false;

                //thread exists, return thread
                return(new DataResponse <ThreadDto>()
                       .WithData(existingThreadDto));
            }

            //thread doesn't exist, so we need to create a record for it

            //find existing namespace to add this thread to
            var namespaceEntity = await _context.Namespaces.Include(n => n.Threads).SingleOrDefaultAsync(n =>
                                                                                                         n.NamespaceId == initThreadDto.NamespaceId);

            //check if namespace exists
            if (namespaceEntity == null)
            {
                return(new Response().WithError("*", "Namespace doesn't exist"));
            }

            //namespace exists, add the new thread to it

            //map thread dto to entity
            var threadEntity = _mapper.Map <Thread>(initThreadDto);

            threadEntity.CreatedOn = DateTime.UtcNow;
            threadEntity.Views     = 1;

            //add new thread to the namespace
            namespaceEntity.Threads.Add(threadEntity);

            //save
            await _context.SaveChangesAsync();

            var newThreadDto = _mapper.Map <ThreadDto>(threadEntity);

            newThreadDto.IsAdmin = _userService.IsAuthenticated ? threadEntity.Namespace.User.Id == _userService.GetUserId() : false;

            return(new DataResponse <ThreadDto>()
                   .WithData(newThreadDto));
        }
Ejemplo n.º 2
0
        public async Task <Response> GetThreadDtoAsync(InitThreadDto initThreadDto)
        {
            var existingThread = await GetThreadWithEverything(t =>
                                                               t.Namespace.NamespaceId == initThreadDto.NamespaceId && t.ThreadId == initThreadDto.ThreadId);

            if (existingThread == null)
            {
                return(Respond().WithError("*", "Thread doesn't exist"));
            }
            var threadDto = _mapper.Map <ThreadDto>(existingThread);

            threadDto.IsAdmin = _userService.IsAuthenticated ? existingThread.Namespace.User.Id == _userService.GetUserId() : false;

            return(RespondWithData(threadDto));
        }
Ejemplo n.º 3
0
 public async Task <IActionResult> Init(InitThreadDto initThreadDto)
 {
     return(this.GenerateResponse(await _threadService.Init(initThreadDto)));
 }