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