/// <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))); }
/// <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))); }
/// <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))); }
/// <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))); }
/// <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))); }
/// <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))); }