Exemple #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Info")] Type @type)
        {
            if (id != @type.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(@type);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TypeExists(@type.Id))
                    {
                        return(NotFound());
                    }

                    throw;
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(@type));
        }
Exemple #2
0
        public async Task <IActionResult> Create([Bind("Id,Name,Info")] Type @type)
        {
            if (ModelState.IsValid)
            {
                _context.Add(@type);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(@type));
        }
Exemple #3
0
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }
            if (fileExcel != null)
            {
                await using var stream = new FileStream(fileExcel.FileName, FileMode.Create);
                await fileExcel.CopyToAsync(stream);

                using XLWorkbook workbook = new XLWorkbook(stream, XLEventTracking.Disabled);

                // check all worksheets (in current case types)
                foreach (IXLWorksheet worksheet in workbook.Worksheets)
                {
                    // worksheet.Name - type name. Try to find in DB, if not exist - create new one
                    Type newType;
                    var  t = _context.Types.Where(typ => typ.Name.Contains(worksheet.Name)).ToList();

                    if (t.Count > 0)
                    {
                        newType = t.First();
                    }
                    else
                    {
                        newType = new Type {
                            Name = worksheet.Name, Info = "from EXCEL"
                        };
                        // add to context
                        _context.Types.Add(newType);
                    }
                    // check all rows
                    foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                    {
                        try
                        {
                            var course = new Course
                            {
                                Price = Convert.ToDecimal(row.Cell(1).Value),
                                Name  = row.Cell(2).Value.ToString(),
                                Info  = row.Cell(3).Value.ToString(),
                                Type  = newType,
                            };

                            var teacherName = row.Cell(4).Value.ToString();
                            var subjectName = row.Cell(5).Value.ToString();
                            if (teacherName?.Length <= 0 || subjectName?.Length <= 0)
                            {
                                continue;
                            }
                            Teacher teacher;
                            Subject subject;

                            // Find subject in case exists, else - add
                            var subjectsWithName = _context.Subjects.Where(sbj => sbj.Name.Contains(subjectName));
                            if (subjectsWithName.Any())
                            {
                                subject = subjectsWithName.First();
                            }
                            else
                            {
                                subject = new Subject {
                                    Name = subjectName
                                };
                                _context.Add(subject);
                            }

                            // Find teacher in case exists, else - add
                            var teachersWithName = _context.Teachers.Where(tr => tr.Name.Contains(teacherName) &&
                                                                           tr.Subject.Name.Contains(subject.Name)).ToList();
                            if (teachersWithName.Count > 0)
                            {
                                teacher = teachersWithName.First();
                            }
                            else
                            {
                                teacher = new Teacher {
                                    Name = teacherName, Info = "from EXCEL", Subject = subject
                                };
                                _context.Add(teacher);
                            }

                            course.Teacher = teacher;
                            _context.Courses.Add(course);
                        }
                        catch (Exception)
                        {
                            // logging
                        }
                    }
                }
            }

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }