private UIElement Create(IEnumerable <string> countries, IEnumerable <string> sports) { ParticipantCreateViewModel viewModel = new ParticipantCreateViewModel(countries, sports); ParticipantCreateControl control = new ParticipantCreateControl(viewModel); viewModel.ParticipantCreated += (s, e) => { ParticipantBaseModel participantModel = e.Participant; ParticipantBaseDTO participantDTO = Mapper.Map <ParticipantBaseModel, ParticipantBaseDTO>(participantModel); using (IParticipantService service = factory.CreateParticipantService()) { ServiceMessage serviceMessage = service.Create(participantDTO); RaiseReceivedMessageEvent(serviceMessage.IsSuccessful, serviceMessage.Message); if (serviceMessage.IsSuccessful) { Notify(); viewModel.ParticipantName = String.Empty; } } }; return(control); }
//TODO //Create and Update have much same code public ServiceMessage Create(ParticipantBaseDTO participantBaseDTO) { string message; bool success = true; string sportName = participantBaseDTO.SportName; string countryName = participantBaseDTO.CountryName; string participantName = participantBaseDTO.Name; try { SportEntity sportEntity = unitOfWork.Sports.Get(sportName); CountryEntity countryEntity = unitOfWork.Countries.Get(countryName); if (sportEntity != null && countryEntity != null) { bool exists = unitOfWork.Participants.Exists(participantName, sportEntity.Id, countryEntity.Id); if (!exists) { ParticipantEntity participantEntity = new ParticipantEntity { Name = participantName, CountryId = countryEntity.Id, SportId = sportEntity.Id }; unitOfWork.Participants.Add(participantEntity); unitOfWork.Commit(); message = "Created new participant"; } else { message = "Such participant already exists"; success = false; } } else { message = "Such sport or country was not found"; success = false; } } catch (Exception ex) { message = ExceptionMessageBuilder.BuildMessage(ex); success = false; } return(new ServiceMessage(message, success)); }
private void Delete(ParticipantBaseModel participantBaseModel) { ParticipantBaseDTO participantBaseDTO = Mapper.Map <ParticipantBaseModel, ParticipantBaseDTO>(participantBaseModel); using (IParticipantService service = factory.CreateParticipantService()) { ServiceMessage serviceMessage = service.Delete(participantBaseDTO); RaiseReceivedMessageEvent(serviceMessage); if (serviceMessage.IsSuccessful) { Notify(); } } }
public ServiceMessage Delete(ParticipantBaseDTO participantBaseDTO) { string message; bool success = true; string sportName = participantBaseDTO.SportName; string countryName = participantBaseDTO.CountryName; string participantName = participantBaseDTO.Name; try { SportEntity sportEntity = unitOfWork.Sports.Get(sportName); CountryEntity countryEntity = unitOfWork.Countries.Get(countryName); if (sportEntity != null && countryEntity != null) { ParticipantEntity participantEntity = unitOfWork.Participants.Get(participantName, sportEntity.Id, countryEntity.Id); if (participantEntity != null) { unitOfWork.Participants.Remove(participantEntity); unitOfWork.Commit(); message = "Deleted participant"; } else { message = "Such participant doesn't exist"; success = false; } } else { message = "Such sport or country was not found"; success = false; } } catch (Exception ex) { message = ExceptionMessageBuilder.BuildMessage(ex); success = false; } return(new ServiceMessage(message, success)); }