public void AddCategory(string name) { if (!context.Publication_Categories.Any(c => c.Name == name)) { Publication_category category = new Publication_category() { Name = name }; context.Publication_Categories.Add(category); context.SaveChanges(); } }
protected override void Seed(recenzent.Data.DataContext context) { try { var testAffiliation = new Affiliation() { Name = "brak" }; if (!context.Affliations.Any(a => a.Name == testAffiliation.Name)) { context.Affliations.AddOrUpdate(testAffiliation); } var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var userManager = new UserManager <User>(new UserStore <User>(context)); if (!roleManager.RoleExists("Admin")) { var role = new IdentityRole(); role.Name = "Admin"; roleManager.Create(role); } if (!roleManager.RoleExists("Author")) { var role = new IdentityRole(); role.Name = "Author"; roleManager.Create(role); } if (!roleManager.RoleExists("Reviewer")) { var role = new IdentityRole(); role.Name = "Reviewer"; roleManager.Create(role); } if (userManager.FindByName("Magik") == null) { var user = new User() { UserName = "******", Name = "Wojciech", Surname = "Sendera", Email = "*****@*****.**", RegistrationDate = DateTime.UtcNow, }; userManager.Create(user, "Admin123"); userManager.AddToRole(user.Id, "Admin"); userManager.AddToRole(user.Id, "Author"); userManager.AddToRole(user.Id, "Reviewer"); } Publication_category category1 = new Publication_category() { Name = "Informatyka" }; Publication_category category2 = new Publication_category() { Name = "Matematyka" }; Publication_category category3 = new Publication_category() { Name = "Fizyka" }; if (!context.Publication_Categories.Any(c => c.Name == category1.Name)) { context.Publication_Categories.Add(category1); } if (!context.Publication_Categories.Any(c => c.Name == category2.Name)) { context.Publication_Categories.Add(category2); } if (!context.Publication_Categories.Any(c => c.Name == category3.Name)) { context.Publication_Categories.Add(category3); } ReviewState state1 = new ReviewState() { Name = "Przydzielony" }; ReviewState state2 = new ReviewState() { Name = "Zaakceptowany" }; ReviewState state3 = new ReviewState() { Name = "Cofniêty do poprawy" }; ReviewState state4 = new ReviewState() { Name = "Odrzucony" }; if (!context.ReviewStates.Any(s => s.Name == state1.Name)) { context.ReviewStates.Add(state1); } if (!context.ReviewStates.Any(s => s.Name == state2.Name)) { context.ReviewStates.Add(state2); } if (!context.ReviewStates.Any(s => s.Name == state3.Name)) { context.ReviewStates.Add(state3); } if (!context.ReviewStates.Any(s => s.Name == state4.Name)) { context.ReviewStates.Add(state4); } } catch (System.Data.Entity.Validation.DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Value: \"{1}\", Error: \"{2}\"", ve.PropertyName, eve.Entry.CurrentValues.GetValue <object>(ve.PropertyName), ve.ErrorMessage); } } throw; } context.SaveChanges(); }
public ActionResult EditPub(PublicationViewModel model) { if (ModelState.IsValid) { var ctx = new DataContext(); Publication publication = ctx.Publications.Where(p => p.PublicationId == model.Id).FirstOrDefault(); IUserService userService = new UserService(); string userId = User.Identity.GetUserId(); User currentUser = ctx.Users.Where(u => u.Id == userId).FirstOrDefault(); //User currentUser = userService.GetDBUser(User.Identity.GetUserId()); //Tags string[] tagsSplited = model.Tags.Split(','); for (int i = 0; i < tagsSplited.Length; i++) { tagsSplited[i] = tagsSplited[i].Trim(); } ITagsService tagsService = new TagsService(); tagsService.AddTags(tagsSplited.ToList()); //Publication tags List <PublicationTag> pubTags = new List <PublicationTag>(); for (int i = 0; i < tagsSplited.Length; i++) { Tag tag = tagsService.GetTag(tagsSplited[i]); if (tagsSplited != null) { pubTags.Add(new PublicationTag() { Publication = publication, Tag = tag }); } } ctx.Publication_Tags.AddRange(pubTags); //category ICategoryService categoryService = new CategoryService(); Publication_category category = categoryService.GetCategory(model.Category); #region Sources //sources //string[] sourcesSplited = model.Sources.Split('\n'); //for (int i = 0; i < sourcesSplited.Length; i++) { // sourcesSplited[i] = sourcesSplited[i].Trim(); //} //var sourcesList = sourcesSplited.ToList(); //sourcesList.RemoveAt(sourcesList.Count - 1); //ISourceService sourceService = new SourceService(); //sourceService.AddSources(sourcesList); ////source position //List<SourcePosition> sourcePositions = new List<SourcePosition>(); //for (int i = 0; i < sourcesSplited.Length - 1; i++) { // string sourceName = sourcesSplited[i]; // Source source = ctx.Sources.Where(s => s.Name == sourceName).FirstOrDefault(); // SourcePosition position = new SourcePosition() { // Source = source, // Publication = publication // }; // sourcePositions.Add(position); //} //ctx.SourcePositions.AddRange(sourcePositions); #endregion //File string filePath = Server.MapPath("~/Publications/"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string fileName = model.File.FileName; fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); model.File.SaveAs(filePath + fileName); Data.Model.File file = new Data.Model.File() { Name = fileName, Link_source = filePath + fileName, IsCurrent = true, Publication = publication }; //IFileService fileService = new FileService(); //fileService.AddFile(file); ctx.Files.Add(file); publication.Author = currentUser; publication.Title = model.Title; publication.PublicationTags = pubTags; publication.Description = model.Description; //publication.SourcePositions = sourcePositions; publication.Category = category; publication.Files.Add(file); currentUser.Publications.Add(publication); ctx.Publications.AddOrUpdate(publication); ctx.SaveChanges(); return(RedirectToAction("Index")); } return(View()); }