예제 #1
0
        /// <summary>
        /// Create set of questions.
        /// </summary>
        /// <param name="collection"> List of questions to be created. </param>
        /// <param name="userId"> Id of the user creating list. </param>
        /// <returns> Created list of objects (dtos). </returns>
        public async Task <DtoResponseResult <IEnumerable <DtoPollQuestion> > > CreateAsync(
            IEnumerable <DtoPollQuestion> collection, int userId)
        {
            var entities = _mapper.Map <IEnumerable <PollQuestion> >(collection);

            foreach (var e in entities)
            {
                e.CreatedById = userId;
                e.CreatedDate = DateTime.UtcNow;
                e.Points ??= 0;
            }

            var entitiesResponse = _unitOfWork.PollQuestionRepository.Create(entities);

            foreach (var e in entitiesResponse)
            {
                var points = e.Points;
                if (e != null && points != 0)
                {
                    var poll = _unitOfWork.PollRepository.Get(e.PollId);
                    poll.Points += points;
                    _unitOfWork.PollRepository.Update(poll, e.PollId);
                }
            }

            await _unitOfWork.SaveChangesAsync();

            return(DtoResponseResult <IEnumerable <DtoPollQuestion> > .CreateResponse(
                       _mapper.Map <IEnumerable <DtoPollQuestion> >(entitiesResponse)));
        }
예제 #2
0
        /// <summary>
        /// Updates a particular question.
        /// </summary>
        /// <param name="dto"> Updated version of an object. </param>
        /// <param name="userId"> Id of the user updating question. </param>
        /// <returns> Updated object (dto). </returns>
        public async Task <DtoResponseResult <DtoPollQuestion> > UpdateAsync(
            DtoPollQuestion dto, int userId)
        {
            var entity = await _unitOfWork.PollQuestionRepository.FindAsync(p => p.Id == dto.Id);

            if (entity == null)
            {
                return(DtoResponseResult <DtoPollQuestion> .FailedResponse("Poll question not found"));
            }

            var points = entity.Points;

            _mapper.Map(dto, entity);
            entity.UpdatedDate = DateTime.UtcNow;
            entity.UpdatedById = userId;

            var entityResponse = _unitOfWork.PollQuestionRepository.Update(entity, dto.Id);

            if (entityResponse != null && points != dto.Points && dto.Points != null)
            {
                var poll = _unitOfWork.PollRepository.Get(entity.PollId);
                poll.Points += dto.Points - points;
                _unitOfWork.PollRepository.Update(poll, entity.PollId);
            }

            await _unitOfWork.SaveChangesAsync();

            return(DtoResponseResult <DtoPollQuestion> .CreateResponse(
                       _mapper.Map <DtoPollQuestion>(entityResponse)));
        }
예제 #3
0
        /// <summary>
        /// Create a single question.
        /// </summary>
        /// <param name="dto"> Object to be created. </param>
        /// <param name="userId"> Id of the user creating a question. </param>
        /// <returns> Created question. </returns>
        public async Task <DtoResponseResult <DtoPollQuestion> > CreateAsync(
            DtoPollQuestion dto, int userId)
        {
            var entity = _mapper.Map <PollQuestion>(dto);

            entity.CreatedById = userId;
            entity.CreatedDate = DateTime.UtcNow;
            entity.Points ??= 0;

            var entityResponse = _unitOfWork.PollQuestionRepository.Create(entity);

            var points = entity.Points;

            if (entityResponse != null && points != 0)
            {
                var poll = _unitOfWork.PollRepository.Get(entity.PollId);
                poll.Points += points;
                _unitOfWork.PollRepository.Update(poll, entity.PollId);
            }

            await _unitOfWork.SaveChangesAsync();

            return(DtoResponseResult <DtoPollQuestion> .CreateResponse(
                       _mapper.Map <DtoPollQuestion>(entityResponse)));
        }
예제 #4
0
        /// <summary>
        /// Find all groups.
        /// </summary>
        /// <returns> Response containing list of DtoGroup. </returns>
        public async Task <DtoResponseResult <IEnumerable <DtoGroup> > > GetAll()
        {
            var entities = await _unitOfWork.GroupRepository.GetAllAsync();

            return(DtoResponseResult <IEnumerable <DtoGroup> > .CreateResponse(
                       _mapper.Map <IEnumerable <DtoGroup> >(entities)));
        }
예제 #5
0
        /// <summary>
        /// Find all messages from particular group.
        /// </summary>
        /// <param name="groupId"> Id of the group associated with requested messages. </param>
        /// <returns> Response containing requested messages (dtos). </returns>
        public async Task <DtoResponseResult <IEnumerable <DtoGroupMessage> > > GetAllByGroupId(int groupId)
        {
            var entities = await _unitOfWork.GroupMessageRepository.FindAllAsync(gm => gm.GroupId == groupId);

            return(DtoResponseResult <IEnumerable <DtoGroupMessage> > .CreateResponse(
                       _mapper.Map <IEnumerable <DtoGroupMessage> >(entities)));
        }
예제 #6
0
        /// <summary>
        /// Get all available questions including answers(not sure if it's going to be useful).
        /// </summary>
        /// <returns> All available questions.</returns>
        public async Task <DtoResponseResult <IEnumerable <DtoPollQuestion> > > GetAllIncluded()
        {
            var entities = _unitOfWork.PollQuestionRepository.GetAll()
                           .AsQueryable().Include(p => p.QuestionAnswers);

            return(DtoResponseResult <IEnumerable <DtoPollQuestion> > .CreateResponse(
                       _mapper.Map <IEnumerable <DtoPollQuestion> >(entities)));
        }
예제 #7
0
        /// <summary>
        /// Get whole answer set from the particular question.
        /// </summary>
        /// <param name="questionId"> Desired answers' question Id. </param>
        /// <returns> Set of particular answers. </returns>
        public async Task <DtoResponseResult <IEnumerable <DtoPollAnswer> > > GetAll(int questionId)
        {
            var entities =
                await _unitOfWork.PollAnswerRepository.FindAllAsync(
                    p => p.PollQuestionId == questionId);

            return(DtoResponseResult <IEnumerable <DtoPollAnswer> > .CreateResponse(
                       _mapper.Map <IEnumerable <DtoPollAnswer> >(entities)));
        }
예제 #8
0
        /// <summary>
        /// Find message of given id.
        /// </summary>
        /// <param name="id"> Id of the message. </param>
        /// <returns> Response containing requested message (dto). </returns>
        public async Task <DtoResponseResult <DtoGroupMessage> > GetById(int id)
        {
            var entity = await _unitOfWork.GroupMessageRepository.FindAsync(gm => gm.Id == id);

            if (entity == null)
            {
                return(DtoResponseResult <DtoGroupMessage> .FailedResponse("Group Message not found"));
            }

            return(DtoResponseResult <DtoGroupMessage> .CreateResponse(_mapper.Map <DtoGroupMessage>(entity)));
        }
예제 #9
0
        /// <summary>
        /// Find a poll with given Id.
        /// </summary>
        /// <param name="pollId"> Poll Id.</param>
        /// <returns> Requested poll.</returns>
        public async Task <DtoResponseResult <DtoPoll> > GetById(int pollId)
        {
            var entity = await _unitOfWork.PollRepository.FindAsync(p => p.Id == pollId);

            if (entity == null)
            {
                return(DtoResponseResult <DtoPoll> .FailedResponse("Poll not found"));
            }
            return(DtoResponseResult <DtoPoll> .CreateResponse(
                       _mapper.Map <DtoPoll>(entity)));
        }
예제 #10
0
        /// <summary>
        /// Create group message.
        /// </summary>
        /// <param name="dto"> Object to be created. </param>
        /// <param name="userId">Id of a user creating the message. </param>
        /// <returns> Response containing created object (dto). </returns>
        public async Task <DtoResponseResult <DtoGroupMessage> > CreateAsync(DtoGroupMessage dto, int userId)
        {
            var entity = _mapper.Map <GroupMessage>(dto);

            entity.CreatedById = userId;
            entity.CreatedDate = DateTime.UtcNow;

            var newEntity = _unitOfWork.GroupMessageRepository.Create(entity);
            await _unitOfWork.SaveChangesAsync();

            return(DtoResponseResult <DtoGroupMessage> .CreateResponse(_mapper.Map <DtoGroupMessage>(newEntity)));
        }
예제 #11
0
        /// <summary>
        /// Find a poll with given Id including questions and answers.
        /// </summary>
        /// <param name="pollId"> Question Id.</param>
        /// <returns> One requested poll.</returns>
        public async Task <DtoResponseResult <DtoPoll> > GetByIdIncluded(int pollId)
        {
            var entity = _unitOfWork.PollRepository.FindBy(p => p.Id == pollId).AsQueryable().Include(p => p.PollQuestions)
                         .ThenInclude(q => q.QuestionAnswers).SingleOrDefault();

            if (entity == null)
            {
                return(DtoResponseResult <DtoPoll> .FailedResponse("Poll not found"));
            }
            return(DtoResponseResult <DtoPoll> .CreateResponse(
                       _mapper.Map <DtoPoll>(entity)));
        }
예제 #12
0
        /// <summary>
        /// Create a single answer.
        /// </summary>
        /// <param name="dto"> Object to be created. </param>
        /// <param name="userId"> Id of the user creating an answer. </param>
        /// <returns> Created answer. </returns>
        public async Task <DtoResponseResult <DtoPollAnswer> > CreateAsync(DtoPollAnswer dto, int userId)
        {
            var entity = _mapper.Map <PollAnswer>(dto);

            entity.CreatedById = userId;
            entity.CreatedDate = DateTime.UtcNow;
            entity.Correctness ??= false;

            var entityResponse = _unitOfWork.PollAnswerRepository.Create(entity);
            await _unitOfWork.SaveChangesAsync();

            return(DtoResponseResult <DtoPollAnswer> .CreateResponse(
                       _mapper.Map <DtoPollAnswer>(entityResponse)));
        }
예제 #13
0
        /// <summary>
        /// Create Entity from Dto asynchronously.
        /// </summary>
        /// <param name="dto">New object</param>
        /// <param name="userId">Id of user creating this particular object.</param>
        /// <returns>Response that contains new created object.</returns>
        public async Task <DtoResponseResult <DtoPoll> > CreateAsync(DtoPoll dto, int userId)
        {
            var entity = _mapper.Map <Poll>(dto);

            entity.CreatedById = userId;
            entity.CreatedDate = DateTime.UtcNow;
            entity.Points      = 0;

            var entityResponse = _unitOfWork.PollRepository.Create(entity);
            await _unitOfWork.SaveChangesAsync();

            return(DtoResponseResult <DtoPoll> .CreateResponse(
                       _mapper.Map <DtoPoll>(entityResponse)));
        }
예제 #14
0
        /// <summary>
        /// Create set of answers.
        /// </summary>
        /// <param name="collection"> Set of answers to be created. </param>
        /// <param name="userId"> Id of the user creating list of answers. </param>
        /// <returns> Created list (dto). </returns>
        public async Task <DtoResponseResult <IEnumerable <DtoPollAnswer> > > CreateAsync(IEnumerable <DtoPollAnswer> collection, int userId)
        {
            var entities = _mapper.Map <IEnumerable <PollAnswer> >(collection);

            foreach (var e in entities)
            {
                e.CreatedById = userId;
                e.CreatedDate = DateTime.UtcNow;
                e.Correctness ??= false;
            }

            var entitiesResponse = _unitOfWork.PollAnswerRepository.Create(entities);
            await _unitOfWork.SaveChangesAsync();

            return(DtoResponseResult <IEnumerable <DtoPollAnswer> > .CreateResponse(
                       _mapper.Map <IEnumerable <DtoPollAnswer> >(entitiesResponse)));
        }
예제 #15
0
        /// <summary>
        /// Update particular group.
        /// </summary>
        /// <param name="dto"> Updated version of an object. </param>
        /// <param name="userId"> Id of the user updating group. </param>
        /// <returns> Response containing created group (dto). </returns>
        public async Task <DtoResponseResult <DtoGroup> > UpdateAsync(DtoGroup dto, int userId)
        {
            var entity = await _unitOfWork.GroupRepository.FindAsync(g => g.Id == dto.Id);

            if (entity == null)
            {
                return(DtoResponseResult <DtoGroup> .FailedResponse("Group not found"));
            }

            _mapper.Map(dto, entity);
            entity.UpdatedById = userId;
            entity.UpdatedDate = DateTime.UtcNow;

            var newEntity = _unitOfWork.GroupRepository.Update(entity, dto.Id);
            await _unitOfWork.SaveChangesAsync();

            return(DtoResponseResult <DtoGroup> .CreateResponse(_mapper.Map <DtoGroup>(newEntity)));
        }
예제 #16
0
        /// <summary>
        /// Update given answer.
        /// </summary>
        /// <param name="dto"> Updated version of an object. </param>
        /// <param name="userId"> Id of the user updating an answer. </param>
        /// <returns> Created object (dto).</returns>
        public async Task <DtoResponseResult <DtoPollAnswer> > UpdateAsync(DtoPollAnswer dto, int userId)
        {
            var entity = await _unitOfWork.PollAnswerRepository.FindAsync(p => p.Id == dto.Id);

            if (entity == null)
            {
                return(DtoResponseResult <DtoPollAnswer> .FailedResponse("Answer not found"));
            }

            _mapper.Map(dto, entity);
            entity.UpdatedDate = DateTime.UtcNow;
            entity.UpdatedById = userId;

            var entityResponse = _unitOfWork.PollAnswerRepository.Update(entity, dto.Id);
            await _unitOfWork.SaveChangesAsync();

            return(DtoResponseResult <DtoPollAnswer> .CreateResponse(
                       _mapper.Map <DtoPollAnswer>(entityResponse)));
        }