public IActionResult Add(SavePublicationStringVM savePublicationString) { SavePublicationVM savePublication = new SavePublicationVM { Id = savePublicationString.publicationId, TemplateId = savePublicationString.TemplateId, Title = savePublicationString.Title, BranchesOfKnowledge = Newtonsoft.Json.JsonConvert.DeserializeObject <List <BranchOfKnowledge> >(savePublicationString.BranchesOfKnowledge), Authors = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Author> >(savePublicationString.Authors), FieldsValue = Newtonsoft.Json.JsonConvert.DeserializeObject <List <FieldValueVM> >(savePublicationString.FieldsValue) }; //TODO move mapping to service (or not move :D) foreach (var file in Request.Form.Files) { if (file.Length > 0) { string path = @"PublicationsFiles\" + Guid.NewGuid(); string name = file.FileName; FileProperties fileProperties = new FileProperties() { FilePath = path, FileName = name }; if (!Directory.Exists("PublicationsFiles")) { Directory.CreateDirectory("PublicationsFiles"); } using (FileStream fs = System.IO.File.Create(path)) { file.CopyTo(fs); } FieldValueVM fieldValue = new FieldValueVM() { FieldType = FieldType.File, Name = file.Name, Value = Newtonsoft.Json.JsonConvert.SerializeObject(fileProperties), isChecked = false }; savePublication.FieldsValue.Add(fieldValue); } } bool isDone; if (savePublication.Id == 0) { isDone = publicationService.AddPublication(savePublication); } else { isDone = publicationService.EditPublication(savePublication); } if (isDone) { return(Json(new { success = true, message = "Zapisano pomyślnie!" })); } else { return(Json(new { success = false, message = "Wystąpił błąd w zapisie!" })); } }
public ActionResult AddPub(PublicationViewModel model) { if (ModelState.IsValid) { Publication publication = new Publication(); //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 }); } } //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 }; publication.AuthoId = User.Identity.GetUserId(); publication.Title = model.Title; publication.PublicationTags = pubTags; publication.Description = model.Description; //publication.SourcePositions = sourcePositions; publication.CategoryId = category.Publication_categoryId; publication.Files.Add(file); IPublicationService pubService = new PublicationService(); pubService.AddPublication(publication); return(RedirectToAction("Index")); } return(View()); }