public void Init(EducationalDirection direction)
        {
            Id                = direction.Id;
            Name              = direction.Name;
            PaidPlacesCount   = direction.PaidPlacesCount;
            BudgetPlacesCount = direction.BudgetPlacesCount;
            PeriodOfStudy     = direction.PeriodOfStudy;
            Code              = direction.Code;
            Level             = direction.Level;
            Form              = direction.EducationalForm;
            PageUrl           = direction.PageUrl;
            UniversityId      = direction.EducationalDivision.UniversityId;
            UniversityName    = direction.EducationalDivision.University.Name;
            Price             = direction.Price;

            DivisionId   = direction.EducationalDivisionId;
            DivisionName = direction.EducationalDivision.Name;

            Subjects = direction.SubjectScores?
                       .Select(s => new SubjectScoreDto
            {
                Id          = s.Id,
                MinScore    = s.MinimumScore,
                SubjectName = s.Subject.Name
            }).ToList();
        }
Beispiel #2
0
 public async Task <EducationalDirection> Create(EducationalDirection entity)
 {
     return(await this.connection.QueryFirstAsync <EducationalDirection>(
                @" INSERT INTO EducationalDirections (Code, Name) VALUES(@Code, @Name);
                   SELECT * FROM StudentCards where Code = @Code;",
                entity));
 }
Beispiel #3
0
        public async Task <EducationalDirection> Update(EducationalDirection entity)
        {
            return(await this.connection.QueryFirstAsync <EducationalDirection>(
                       @"UPDATE EducationalDirections
                            SET Code = @Code,
	                        Name = @Name
                         WHERE Code = @Code;

                         SELECT * FROM Groups where id = @id",
                       entity));
        }
        public async Task <IActionResult> CreateOrUpdate(DirectionViewModel model)
        {
            EducationalDirection direction;

            if (!ModelState.IsValid)
            {
                return(View("Create", model));
            }
            TrimStringProperties(ref model);
            if (model.Id > 0)
            {
                direction = await _dbContext.Directions
                            .Include(d => d.EducationalDivision.University)
                            .Include(d => d.SubjectScores)
                            .ThenInclude(s => s.Subject)
                            .FirstOrDefaultAsync(d => d.Id == model.Id);

                if (direction == null)
                {
                    return(NotFound("CreateOrUpdate", model.Id));
                }
                direction.ModifiedDateTime = DateTime.Now;
            }
            else
            {
                direction = new EducationalDirection();
                direction.CreateDateTime        = direction.ModifiedDateTime = DateTime.Now;
                direction.EducationalDivisionId = model.DivisionId;
                _dbContext.Directions.Add(direction);
            }
            direction.Name              = model.Name;
            direction.PaidPlacesCount   = model.PaidPlacesCount;
            direction.PeriodOfStudy     = model.PeriodOfStudy;
            direction.Level             = model.Level;
            direction.BudgetPlacesCount = model.BudgetPlacesCount;
            direction.Code              = model.Code;
            direction.EducationalForm   = model.Form;
            direction.Price             = model.Price;
            direction.PageUrl           = model.PageUrl;
            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                return(Error("CreateOrUpdate", ex));
            }

            return(RedirectToAction("Edit", new { id = direction.Id }));
        }
Beispiel #5
0
        private async Task <ResponceExcelService> SaveToDatabase(ICollection <ImportDataDto> dataDtos)
        {
            var responce = new ResponceExcelService();

            try
            {
                var subjects = dataDtos
                               .SelectMany(x => x.Subjects.Select(s => s.Name))
                               .Distinct().ToList();
                var subjectsInDb = _dbContext.Subjects.Select(s => s.Name).ToList();

                if (!subjects.All(s => subjectsInDb.Any(x => x == s)))
                {
                    responce.IsOk        = false;
                    responce.ErrorMesage = "В файле присутствуют предметы, которых нет в списке. Скачайте шаблон и заполните его.";
                    return(responce);
                }

                ICollection <University> universities = new List <University>();
                var data = dataDtos.GroupBy(x => x.UniversityName, (u, us) => new
                {
                    UniversityName = u,
                    Divisions      = us.GroupBy(d => d.DivisionName, (d, ds) => new
                    {
                        DivisionName = d,
                        Directions   = ds.Select(dir => new
                        {
                            DirectionName = dir.DirectionName,
                            Budjet        = dir.BudgetPlacesCount,
                            Payment       = dir.PaidPlacesCount,
                            Price         = dir.Price,
                            Level         = dir.Level,
                            Form          = dir.Form,
                            Code          = dir.Code,
                            Period        = dir.PeriodOfStudy,
                            Subjects      = dir.Subjects
                        }).ToList()
                    }).ToList()
                }).ToList();
                var now = DateTime.Now;
                foreach (var item in data)
                {
                    University university = await _dbContext.Universities
                                            .Include(u => u.Divisions)
                                            .FirstOrDefaultAsync(u => u.Name == item.UniversityName);

                    if (university == null)
                    {
                        university                = new University();
                        university.Name           = item.UniversityName;
                        university.CreateDateTime = university.ModifiedDateTime = now;
                        university.Divisions      = new List <EducationalDivision>();
                        _dbContext.Universities.Add(university);
                        await _dbContext.SaveChangesAsync();
                    }

                    foreach (var division in item.Divisions)
                    {
                        EducationalDivision educationalDivision = await _dbContext.Divisions
                                                                  .Include(x => x.EducationalDirections)
                                                                  .FirstOrDefaultAsync(d => d.Name == division.DivisionName && d.UniversityId == university.Id);

                        if (educationalDivision == null)
                        {
                            educationalDivision                       = new EducationalDivision();
                            educationalDivision.Name                  = division.DivisionName;
                            educationalDivision.CreateDateTime        = educationalDivision.ModifiedDateTime = now;
                            educationalDivision.EducationalDirections = new List <EducationalDirection>();
                            university.Divisions.Add(educationalDivision);
                            await _dbContext.SaveChangesAsync();
                        }

                        foreach (var direction in division.Directions)
                        {
                            var form  = direction.Form.GetEnumFromString <EducationalFormEnum>();
                            var level = direction.Level.GetEnumFromString <EducationalLevelEnum>();
                            EducationalDirection educationalDirection = await _dbContext.Directions
                                                                        .Include(x => x.SubjectScores)
                                                                        .FirstOrDefaultAsync(x => x.Name == direction.DirectionName &&
                                                                                             x.EducationalDivisionId == educationalDivision.Id &&
                                                                                             x.EducationalForm == form &&
                                                                                             x.Level == level);

                            if (educationalDirection == null)
                            {
                                educationalDirection                = new EducationalDirection();
                                educationalDirection.Name           = direction.DirectionName;
                                educationalDirection.CreateDateTime = educationalDirection.ModifiedDateTime = now;

                                educationalDivision.EducationalDirections.Add(educationalDirection);
                            }
                            else
                            {
                                educationalDirection.ModifiedDateTime = now;
                            }

                            educationalDirection.Level             = level;
                            educationalDirection.EducationalForm   = form;
                            educationalDirection.BudgetPlacesCount = (int)direction.Budjet;
                            educationalDirection.PaidPlacesCount   = (int)direction.Payment;
                            educationalDirection.PeriodOfStudy     = direction.Period;
                            educationalDirection.Price             = (int)direction.Price;
                            educationalDirection.Code = direction.Code;

                            if (educationalDirection.SubjectScores?.Any() == true)
                            {
                                educationalDirection.SubjectScores.Clear();
                            }
                            else if (educationalDirection.SubjectScores == null)
                            {
                                educationalDirection.SubjectScores = new List <SubjectScore>();
                            }
                            await _dbContext.SaveChangesAsync();

                            foreach (var subjectDto in direction.Subjects)
                            {
                                var subject = await _dbContext.Subjects.FirstOrDefaultAsync(x => x.Name == subjectDto.Name);

                                if (subject != null)
                                {
                                    var subjectScore = new SubjectScore();
                                    subjectScore.MinimumScore   = (int)subjectDto.Score;
                                    subjectScore.CreateDateTime = subjectScore.ModifiedDateTime = now;
                                    subjectScore.Subject        = subject;
                                    educationalDirection.SubjectScores.Add(subjectScore);
                                }
                            }
                        }
                    }
                }
                await _dbContext.SaveChangesAsync();

                responce.IsOk = true;
                return(responce);
            }
            catch (Exception ex)
            {
                var errorMessage = $"SaveToDatabase.\n{ex}";
                _logger.LogWarning(errorMessage);
                responce.IsOk        = false;
                responce.ErrorMesage = ex.Message;
                return(responce);
            }
        }