public async Task <ActionResult <ParticipationViewModel> > GetParticipationAsync(Guid personId, Guid projectId) { if (personId == Guid.Empty || projectId == Guid.Empty) { return(BadRequest("No valid id.")); } try { TaskResult <Participation> result = await participationService.GetParticipationAsync(personId, projectId); if (!result.Succeeded) { if (result.Data == null) { return(NoContent()); } } ParticipationViewModel participationViewModel = ParticipationViewModel.CreateVm(result.Data); return(Ok(participationViewModel)); } catch (Exception ex) { string message = GetType().Name + "Error in " + nameof(GetParticipationAsync); logger.LogError(ex, message); return(UnprocessableEntity(new ErrorViewModel { Type = Type.Error, Message = message })); } }
public async Task <ActionResult <ParticipationViewModel> > Add(ParticipationViewModel participationViewModel) { if (!ModelState.IsValid) { return(CustomResponse(ModelState)); } await _participationService.Add(_mapper.Map <Participation>(participationViewModel)); return(CustomResponse(participationViewModel)); }
public async Task <ActionResult <ParticipationViewModel> > SaveParticipationAsync( [FromBody] ParticipationViewModel participationViewModel) { if (participationViewModel?.Person == null || participationViewModel.Project == null) { return(BadRequest("No valid participation received")); } TaskResult <Participation> result = null; try { Participation participation = (await participationService.GetParticipationAsync( participationViewModel.Person.Id, participationViewModel.Project.Id)).Data; if (participation != null) { participation.Active = true; participation.LastEditBy = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity); result = await participationService.UpdateParticipationAsync(participation); } else { participation = ParticipationViewModel.CreateParticipation(participationViewModel); participation.Active = true; participation.LastEditBy = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity); if (participation.Id == Guid.Empty) { //create participation result = await participationService.AddParticipationAsync(participation); } } if (result == null || !result.Succeeded) { return(UnprocessableEntity(new ErrorViewModel { Type = Type.Error, Message = result?.Message })); } return(Ok(ParticipationViewModel.CreateVm(result.Data))); } catch (Exception ex) { string message = GetType().Name + "Error in " + nameof(SaveParticipationAsync); logger.LogError(ex, message); return(UnprocessableEntity(new ErrorViewModel { Type = Type.Error, Message = message })); } }
/// <summary> /// Fetches the participation record whose id matches the given parameter. /// </summary> /// <param name="id">The participation id.</param> /// <returns>If found, returns ParticipationViewModel, if not returns null.</returns> public ParticipationViewModel Get(string id) { var query = _unitOfWork.ParticipationRepository.GetById(id); if (query == null) { throw new ResourceNotFoundException() { ExceptionMessage = "The Participation was not found." }; } ParticipationViewModel result = query; return(result); }
public async Task <IActionResult> Update(Guid id, ParticipationViewModel participationViewModel) { var participationAtualizacao = await GetParticipation(id); if (!ModelState.IsValid) { return(CustomResponse(ModelState)); } participationAtualizacao.FirstName = participationViewModel.FirstName; participationAtualizacao.LastName = participationViewModel.LastName; participationAtualizacao.Value = participationViewModel.Value; await _participationService.Update(_mapper.Map <Participation>(participationAtualizacao)); return(CustomResponse(participationViewModel)); }
public async Task <IActionResult> Participate([FromBody] ParticipationViewModel viewModel) { await _service.Participate(viewModel.AppointmentId.Value, viewModel.ParticipantId.Value); return(Ok()); }
public RemoveParticipationFromShooterDialogMessage(int shooterId, ParticipationViewModel participation) { ShooterId = shooterId; Participation = participation; }
public async Task <ActionResult <ParticipationViewModel> > UpdateParticipationAsync( [FromBody] ParticipationViewModel participationViewModel) { if (participationViewModel?.Person == null || participationViewModel.Person.Id == Guid.Empty || participationViewModel.Project == null || participationViewModel.Project.Id == Guid.Empty) { return(BadRequest("No valid participation received")); } TaskResult <Participation> result = new TaskResult <Participation>(); try { Participation oldParticipation = (await participationService .GetParticipationAsync(participationViewModel.Person.Id, participationViewModel.Project.Id) ).Data; if (!oldParticipation.RowVersion.SequenceEqual(participationViewModel.RowVersion)) { return(BadRequest("Outdated entity received")); } Participation updatedParticipation = ParticipationViewModel.CreateParticipation(participationViewModel); if (oldParticipation.ProjectId != updatedParticipation.ProjectId) { result.Message = "A participation cannot change project"; return(BadRequest(result)); } if (oldParticipation.PersonId != updatedParticipation.PersonId) { result.Message = "A participation cannot change user"; return(BadRequest(result)); } oldParticipation.Availabilities = updatedParticipation.Availabilities; oldParticipation.MaxWorkingHoursPerWeek = updatedParticipation.MaxWorkingHoursPerWeek; oldParticipation.Active = updatedParticipation.Active; oldParticipation.Remark = updatedParticipation.Remark; oldParticipation.Person = null; oldParticipation.Project = null; oldParticipation.LastEditBy = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity); result = await participationService.UpdateParticipationAsync(oldParticipation); if (!result.Succeeded) { return(UnprocessableEntity(new ErrorViewModel { Type = Type.Error, Message = result.Message })); } result.Data.Person = updatedParticipation.Person; result.Data.Project = updatedParticipation.Project; return(Ok(ParticipationViewModel.CreateVm(result.Data))); } catch (Exception ex) { string message = GetType().Name + "Error in " + nameof(UpdateParticipationAsync); logger.LogError(ex, message); return(UnprocessableEntity(new ErrorViewModel { Type = Type.Error, Message = message })); } }
public async Task <IHttpActionResult> Participate([FromBody] ParticipationViewModel participate) { dynamic expando = new ExpandoObject(); var apiResponse = new ApiResponse { Success = false, Message = "Bad Request", Data = expando }; try { if (participate == null) { expando.Error = new List <string>() { "Body request data should be as documented" }; apiResponse.Message = "Missing or unknown request body"; apiResponse.Data = expando; _logger.LogWarn("Participate validation Error", "Body request not found or unknown"); return(Content(HttpStatusCode.BadRequest, apiResponse)); } if (!ModelState.IsValid) { var allErrors = ModelState.Values.SelectMany(v => v.Errors); var errorList = allErrors.Select(error => error.ErrorMessage); expando.Error = errorList; apiResponse.Message = "Validation error occured"; apiResponse.Data = expando; _logger.LogWarn("Participate validation Error", string.Join(", ", errorList)); return(Content(HttpStatusCode.BadRequest, apiResponse)); } var dto = new ParticipationDto { Id = Guid.NewGuid(), EmailHash = StringUtility.Md5HashEncode(participate.Email.ToLower()), Email = participate.Email, SiteId = _siteService.GetSiteByCulture(participate.Culture)?.Id, RetailerConsent = participate.RetailerConsent, NewsletterOptin = participate.NewsletterOptin }; var crmResponse = await _journeyService.ParticipateAsync(dto, participate.Culture); var site = _siteService.GetSiteByCulture(participate.Culture); expando.Description = "Your participate has been taken into account."; expando.ConsumerId = crmResponse.Item1 ? crmResponse.Item2 : string.Empty; expando.ParticipationId = dto.Id; apiResponse.Success = true; apiResponse.Message = crmResponse.Item1 ? "Participate successfull" : "Your participate has been considered but CRM sync has failed"; apiResponse.Data = expando; _logger.LogTrace(apiResponse.Message, $"Ok: {dto.Id}"); return(Ok(apiResponse)); } catch (Exception e) { expando.Error = e.Message; apiResponse.Success = false; apiResponse.Message = $"Error occured in {e.Source}"; apiResponse.Data = expando; _logger.LogError(e.Message, e); return(Content(HttpStatusCode.InternalServerError, apiResponse)); } }