Exemplo n.º 1
0
        public IPractice CastToDbModel(PracticeViewModel practiceVm)
        {
            var practiceDb = new Practice
            {
                Id = practiceVm.Id,
                ConcurrencyStamp = practiceVm.ConcurrencyStamp,
                Description      = practiceVm.Description,
                MaxParticipants  = practiceVm.MaxParticipants
            };

            DateTime baseDate = new DateTime();

            if (!string.IsNullOrEmpty(practiceVm.PracticeDate))
            {
                bool res = DateTime.TryParse(practiceVm.PracticeDate, out baseDate);
                if (!res)
                {
                    throw new Exception("Cannot parse practice date in practice service.");
                }
            }

            if (!string.IsNullOrEmpty(practiceVm.Begins))
            {
                try
                {
                    TimeSpan beginTime = Convert.ToDateTime(practiceVm.Begins).TimeOfDay;
                    var      beginning = baseDate;
                    beginning        += beginTime;
                    practiceDb.Begins = beginning;
                }
                catch (Exception ex)
                {
                    throw new Exception($"Cannot parse begins time in practice service.     {ex.Message}");
                }
            }

            if (!string.IsNullOrEmpty(practiceVm.Ends))
            {
                try
                {
                    TimeSpan endTime = Convert.ToDateTime(practiceVm.Ends).TimeOfDay;
                    var      ending  = baseDate;
                    ending         += endTime;
                    practiceDb.Ends = ending;
                }
                catch (Exception ex)
                {
                    throw new Exception($"Cannot parse end time in practice service. {ex.Message}");
                }
            }

            return(practiceDb);
        }
Exemplo n.º 2
0
        public PracticeViewModel CastToViewModel(IPractice practiceD)
        {
            Practice domainPractice = practiceD as Practice;

            if (domainPractice == null)
            {
                return(new PracticeViewModel());
            }

            var practiceVm = new PracticeViewModel
            {
                Id = domainPractice.Id,
                ConcurrencyStamp = domainPractice.ConcurrencyStamp,
                Description      = domainPractice.Description,
                MaxParticipants  = domainPractice.MaxParticipants
            };

            practiceVm.PracticeDate = domainPractice.Begins.ToString("d");
            practiceVm.Begins       = domainPractice.Begins.ToString("t");
            practiceVm.Ends         = domainPractice.Ends.ToString("t");

            return(practiceVm);
        }