예제 #1
0
        private UIElement Add(IEnumerable <string> sports, IEnumerable <TournamentBaseModel> tournaments, IEnumerable <EventBaseModel> events)
        {
            CoefficientCreateViewModel viewModel = new CoefficientCreateViewModel(sports, tournaments, events);
            CoefficientCreateControl   control   = new CoefficientCreateControl(viewModel);

            viewModel.CoefficientCreated += (s, e) =>
            {
                CoefficientCreateModel coefficientBaseModel = e.Coefficient;
                CoefficientCreateDTO   coefficientBaseDTO   = Mapper.Map <CoefficientCreateModel, CoefficientCreateDTO>(coefficientBaseModel);

                using (ICoefficientService service = factory.CreateCoefficientService())
                {
                    ServiceMessage serviceMessage = service.Create(coefficientBaseDTO);
                    RaiseReceivedMessageEvent(serviceMessage.IsSuccessful, serviceMessage.Message);

                    if (serviceMessage.IsSuccessful)
                    {
                        viewModel.CoefficientValue = 0;
                        viewModel.Description      = String.Empty;
                        Notify();
                    }
                }
            };

            return(control);
        }
        //TODO
        //merge create and update without code duplication
        public ServiceMessage Create(CoefficientCreateDTO coefficientCreateDTO)
        {
            string message = "";
            bool   success = true;

            if (success = IsValid(coefficientCreateDTO, ref message))
            {
                string   sportName      = coefficientCreateDTO.SportName;
                string   tournamentName = coefficientCreateDTO.TournamentName;
                DateTime dateOfEvent    = coefficientCreateDTO.DateOfEvent;
                List <ParticipantBaseDTO> participants = coefficientCreateDTO.Participants;
                decimal value       = coefficientCreateDTO.Value;
                string  description = coefficientCreateDTO.Description;

                try
                {
                    IEnumerable <ParticipantEntity> participantEntities = participants
                                                                          .Select(p => unitOfWork.Participants.Get(p.Name, p.SportName, p.CountryName));

                    EventEntity eventEntity = unitOfWork.Events.Get(sportName, tournamentName, dateOfEvent, participantEntities);
                    bool        exists      = unitOfWork.Coefficients.Exists(eventEntity.Id, description);

                    if (!exists)
                    {
                        CoefficientEntity coefficientEntity = new CoefficientEntity
                        {
                            EventId     = eventEntity.Id,
                            Value       = value,
                            Description = description
                        };

                        unitOfWork.Coefficients.Add(coefficientEntity);
                        unitOfWork.Commit();

                        message = "Coefficient added";
                    }
                    else
                    {
                        message = "Such coefficient already exists";
                        success = false;
                    }
                }
                catch (Exception ex)
                {
                    message = ExceptionMessageBuilder.BuildMessage(ex);
                    success = false;
                }
            }

            return(new ServiceMessage(message, success));
        }
        private bool IsValid(CoefficientCreateDTO coefficientCreateDTO, ref string message)
        {
            bool isValid = true;

            if (coefficientCreateDTO.Value <= 0)
            {
                message = "Value must be higher than 0";
                isValid = false;
            }
            else if (String.IsNullOrEmpty(coefficientCreateDTO.Description))
            {
                message = "Coefficient must have a description";
                isValid = false;
            }

            return(isValid);
        }
예제 #4
0
 public ServiceMessage Create(CoefficientCreateDTO coefficientCreateDTO)
 {
     return(new ServiceMessage("No permissions", false));
 }