Example #1
0
        private List <RelatedArticlesDto> GetRelatedArticlesFromTaxonomyTermDto(TermDto term, int id, IClinicalGuidelinesAppContext db = null)
        {
            var relatedArticleDtos = new List <RelatedArticlesDto>();

            if (db == null)
            {
                return(relatedArticleDtos);
            }

            var relatedArticles = db
                                  .Articles
                                  .Include(x => x.CurrentArticleRevision)
                                  .Include(x => x.CurrentArticleRevision.SubSectionTab)
                                  .Include(x => x.CurrentArticleRevision.Departments)
                                  .Include(x => x.CurrentArticleRevision.Terms)
                                  .Where(x => x.CurrentArticleRevision.Terms.Any(y => y.Id == term.Id))
                                  .Where(x => x.Id != id)
                                  .Where(x => x.CurrentArticleRevision.Live);

            if (!relatedArticles.Any())
            {
                return(relatedArticleDtos);
            }

            foreach (var relatedArticle in relatedArticles)
            {
                var relatedArticleDto = new RelatedArticlesDto()
                {
                    Id              = relatedArticle.Id,
                    _id             = relatedArticle._id,
                    SubSectionTabId = relatedArticle.CurrentArticleRevision.SubSectionTab.Id,
                    Name            = relatedArticle.CurrentArticleRevision.Title,
                    Departments     = new List <DepartmentDto>(),
                    Term            = new TermDto()
                    {
                        Id   = term.Id,
                        Name = term.Name
                    }
                };

                foreach (var department in relatedArticle.CurrentArticleRevision.Departments)
                {
                    relatedArticleDto.Departments.Add(new DepartmentDto()
                    {
                        Id               = department.Id,
                        _id              = department._id,
                        Name             = department.Name,
                        ShortName        = department.ShortName,
                        ContainerId      = department.ContainerId,
                        BackgroundColour = department.BackgroundColour
                    });
                }

                relatedArticleDtos.Add(relatedArticleDto);
            }

            return(relatedArticleDtos);
        }
        public async Task <Result> AddTerm(TermDto termToAdd)
        {
            var entity = new Term
            {
                TermName     = termToAdd.TermName,
                PeriodInDays = termToAdd.PeriodInDays,
            };

            await _context.Terms.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(Result.Success("Term was created successfully"));
        }
        public async Task <SelectItemVm> GetAllAsSelect(TermDto termDto)
        {
            var vm = new SelectItemVm
            {
                SelectItems = await _context.Terms
                              .Where(x => !x.Deleted)
                              .Select(x => new SelectItemDto {
                    Label = x.TermName, Value = x.Id.ToString()
                })
                              .ToListAsync()
            };

            return(vm);
        }
        public async Task <Result> DeleteTerm(TermDto termToDelete)
        {
            var entity = await _context.Terms
                         .FirstOrDefaultAsync(x => x.Id == termToDelete.Id && !x.Deleted);

            if (entity == null)
            {
                return(Result.Failure(new List <string> {
                    "No valid term found"
                }));
            }

            entity.Deleted = true;

            await _context.SaveChangesAsync();

            return(Result.Success("Term was deleted"));
        }
        public async Task <Result> UpdateTerm(TermDto termToUpdate)
        {
            var entity = await _context.Terms
                         .FirstOrDefaultAsync(x => x.Id == termToUpdate.Id && !x.Deleted);

            if (entity == null)
            {
                return(Result.Failure(new List <string> {
                    "No valid term found"
                }));
            }

            entity.TermName     = termToUpdate.TermName;
            entity.PeriodInDays = termToUpdate.PeriodInDays;

            await _context.SaveChangesAsync();

            return(Result.Success("Term update was successful"));
        }
        public List <TermByDateDto> GetAllTerms(SportObject sportObject)
        {
            var workingHours = sportObject.WorkingHours.ToList();

            if (!workingHours.Any())
            {
                throw new Exception("Sport object doesn't have working hours");
            }

            var maxTimeWorkingHour = sportObject.WorkingHours.Min(wh => wh.CloseTime);

            if (maxTimeWorkingHour.Hours != 0)
            {
                maxTimeWorkingHour = sportObject.WorkingHours.Max(wh => wh.CloseTime);
            }

            var prices = sportObject.Prices.ToList();

            if (!prices.Any())
            {
                throw new Exception("Sport object doesn't have prices");
            }

            var futureDays   = 7;
            var reservations = sportObject.Reservations
                               .Where(r => r.Date >= DateTime.Today && r.Date <= DateTime.Today.AddDays(futureDays))
                               .ToList();

            var termsByDate = new List <TermByDateDto>();

            for (int i = 0; i <= futureDays; i++)
            {
                var date           = DateTime.Today.AddDays(i);
                var day            = (int)date.DayOfWeek == 0 ? 7 : (int)date.DayOfWeek;
                var wh             = workingHours.Find(w => w.Day == day);
                var openTimeHours  = wh.OpenTime.Hours;
                var closeTimeHours = wh.CloseTime.Hours;

                if (closeTimeHours == 0)
                {
                    closeTimeHours = 24;
                }

                var termByDate = new TermByDateDto();
                termByDate.Date = date;

                for (int j = openTimeHours; j < closeTimeHours; j++)
                {
                    var startTime = new TimeSpan(j, 0, 0);
                    var endTime   = startTime.Add(TimeSpan.FromHours(1));

                    if (startTime.Hours == 23)
                    {
                        endTime = new TimeSpan(0, 0, 0);
                    }

                    var price = sportObject.Prices.SingleOrDefault(p => startTime >= p.TimeFrom && startTime < p.TimeTo && p.TimeTo != maxTimeWorkingHour);

                    if (price == null)
                    {
                        price = sportObject.Prices.SingleOrDefault(p => p.TimeTo == maxTimeWorkingHour);
                    }


                    var term = new TermDto
                    {
                        StartTime = startTime,
                        Price     = price.PricePerHour,
                        Status    = "free",
                        IsExpired = false
                    };

                    termByDate.Terms.Add(term);
                }

                termsByDate.Add(termByDate);
            }


            foreach (var res in reservations)
            {
                foreach (var termByDate in termsByDate)
                {
                    foreach (var term in termByDate.Terms)
                    {
                        if (termByDate.Date == res.Date && term.StartTime == res.StartTime)
                        {
                            term.Status = res.Status.Status;
                        }

                        if (termByDate.Date == DateTime.Today && term.StartTime < DateTime.Now.TimeOfDay)
                        {
                            term.Status = Status.Accepted;
                        }
                    }
                }
            }

            return(termsByDate);
        }