Ejemplo n.º 1
0
        public async Task <IActionResult> Create(int publicationnId, [Bind("Id,ProfessorId,PublicationId")] ProfessorPublicationLinker professorPublicationLinker)
        {
            professorPublicationLinker.PublicationId = publicationnId;
            if (ModelState.IsValid)
            {
                _context.Add(professorPublicationLinker);
                await _context.SaveChangesAsync();

                //return RedirectToAction(nameof(Index));
                return(RedirectToAction("Index", "Professor1PublicationLinkers", new { id = publicationnId,
                                                                                       name = _context.Publication.Where(c => c.Id == publicationnId).FirstOrDefault().NamePublication }));
            }
            ViewData["ProfessorId"] = new SelectList(_context.Professor, "Id", "Name", professorPublicationLinker.ProfessorId);
            // ViewData["PublicationId"] = new SelectList(_context.Publication, "Id", "NamePublication", professorPublicationLinker.PublicationId);
            return(RedirectToAction("Index", "Professor1PublicationLinkers", new
            {
                id = publicationnId,
                name = _context.Publication.Where(c => c.Id == publicationnId).FirstOrDefault().NamePublication
            }));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Edit(int id, int publicationId, [Bind("Id,ProfessorId")] ProfessorPublicationLinker professorPublicationLinker)
        {
            professorPublicationLinker.PublicationId = publicationId;
            if (id != professorPublicationLinker.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(professorPublicationLinker);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProfessorPublicationLinkerExists(professorPublicationLinker.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index", "Professor1PublicationLinkers", new
                {
                    id = publicationId,
                    name = _context.Publication.Where(c => c.Id == publicationId).FirstOrDefault().NamePublication
                }));
            }
            ViewData["ProfessorId"]   = new SelectList(_context.Professor, "Id", "Name", professorPublicationLinker.ProfessorId);
            ViewData["PublicationId"] = new SelectList(_context.Publication, "Id", "NamePublication", professorPublicationLinker.PublicationId);
            return(RedirectToAction("Index", "Professor1PublicationLinkers", new
            {
                id = publicationId,
                name = _context.Publication.Where(c => c.Id == publicationId).FirstOrDefault().NamePublication
            }));
        }
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            if (ModelState.IsValid)
            {
                if (fileExcel != null)
                {
                    using (var stream = new FileStream(fileExcel.FileName, FileMode.Create))
                    {
                        await fileExcel.CopyToAsync(stream);

                        using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled))
                        {
                            foreach (IXLWorksheet worksheet in workBook.Worksheets)
                            {
                                DegreeCollection newdegree;
                                var c = (from deg in _context.DegreeCollection
                                         where deg.DegreeName.Contains(worksheet.Name)
                                         select deg).ToList();
                                if (c.Count > 0)
                                {
                                    newdegree = c[0];
                                }
                                else

                                {
                                    newdegree            = new DegreeCollection();
                                    newdegree.DegreeName = worksheet.Name;
                                    _context.DegreeCollection.Add(newdegree);
                                }
                                foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                {
                                    try
                                    {
                                        Professor professor = new Professor();
                                        professor.Name             = row.Cell(1).Value.ToString();
                                        professor.Surname          = row.Cell(2).Value.ToString();
                                        professor.DateOfBirth      = Convert.ToDateTime(row.Cell(3).Value.ToString());
                                        professor.PersonalNumber   = Convert.ToInt32(row.Cell(4).Value.ToString());
                                        professor.PlaceOfWorkingId = Convert.ToInt32(row.Cell(5).Value.ToString());
                                        professor.Degree           = newdegree;

                                        var pr = (from prof in _context.Professor
                                                  where prof.PersonalNumber == professor.PersonalNumber
                                                  select prof).ToList();

                                        if (pr.Count > 0)
                                        {
                                            professor = pr[0];
                                        }
                                        else

                                        {
                                            _context.Professor.Add(professor);
                                        }

                                        int i = 6;
                                        while (row.Cell(i).Value.ToString().Length > 0)
                                        {
                                            Publication publication;
                                            var         p = (from publ in _context.Publication
                                                             where
                                                             (publ.NamePublication.Contains(row.Cell(i).Value.ToString()) &&
                                                              publ.Version == Convert.ToInt32(row.Cell(i + 1).Value.ToString()) &&
                                                              publ.PublishingId == Convert.ToInt32(row.Cell(i + 2).Value.ToString()))
                                                             select publ).ToList();
                                            if (p.Count > 0)
                                            {
                                                publication = p[0];
                                            }
                                            else
                                            {
                                                publication = new Publication();
                                                publication.NamePublication = row.Cell(i).Value.ToString();
                                                publication.Version         = Convert.ToInt32(row.Cell(i + 1).Value.ToString());
                                                publication.PublishingId    = Convert.ToInt32(row.Cell(i + 2).Value.ToString());
                                                publication.PageAmount      = Convert.ToInt32(row.Cell(i + 3).Value.ToString());
                                                _context.Publication.Add(publication);
                                            }
                                            ProfessorPublicationLinker pp = new ProfessorPublicationLinker();
                                            pp.Professor   = professor;
                                            pp.Publication = publication;

                                            var ppp = (from prof in _context.ProfessorPublicationLinker
                                                       where (prof.Professor == professor && prof.Publication == publication)
                                                       select prof).ToList();

                                            if (ppp.Count > 0)
                                            {
                                                pp = ppp[0];
                                            }
                                            else

                                            {
                                                _context.ProfessorPublicationLinker.Add(pp);
                                            }


                                            i += 4;
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                        }
                    }
                }
                await _context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Index)));
        }