public CoreModels.NoteFileContent AddFile(Guid userId, CoreModels.NoteFileContent file) { var typeFile = _dbContext.FileTypes .First(ft => ft.Type == file.FileType); if (typeFile == null) { typeFile = new DbModels.FileType(file.FileType); _dbContext.FileTypes.Add(typeFile); } var dbFile = new DbModels.File(file.FileName, typeFile.Id, typeFile, Guid.Parse(file.NoteId)); var textNote = _dbContext.TextNotes .Include(tn => tn.Note) .First(tn => tn.Id == Guid.Parse(file.NoteId)); textNote.Text += $"![{file.FileId}]({file.FileName})\n"; _dbContext.Entry(textNote).State = EntityState.Modified; _dbContext.Files.Add(dbFile); _dbContext.SaveChanges(); return(new CoreModels.NoteFileContent(dbFile.TextNoteId.ToString(), "file", dbFile.FilePath, dbFile.FileType.Type, dbFile.Id.ToString())); }
public IEnumerable <CoreModels.INoteContent> GetNoteContent(Guid userId, Guid noteId) { var userNote = _dbContext.UserNotes .Include(un => un.Note) .ThenInclude(n => n.TextNote) .Include(un => un.User) .ThenInclude(u => u.Passcode) .Include(un => un.User) .ThenInclude(u => u.UserPhoto) .First(un => un.UserId == userId && un.NoteId == noteId); var content = userNote.Note.TextNote.Text; Regex regexCode = new Regex(@"(?<code>```(.*?)```)|(?<code>`(.*?)`)", RegexOptions.Singleline); Regex regexFiles = new Regex(@"^(?<allstr>!\[(.*?)\]\((?<pathToFile>.*?)\))$", RegexOptions.Multiline); List <string> noCode = new List <string>(); List <string> code = new List <string>(); List <CoreModels.INoteContent> contents = new List <CoreModels.INoteContent>(); int i = 0; foreach (Match m in regexCode.Matches(content)) { code.Add(m.Groups["code"].Value); noCode.Add(content.Substring(i, m.Groups["code"].Index - i)); i = m.Groups["code"].Index + m.Groups["code"].Length; } noCode.Add(content.Substring(i, content.Length - i)); i = 0; while (i < noCode.Count) { int j = 0; foreach (Match m in regexFiles.Matches(noCode[i])) { var dbFile = _dbContext.Files .Include(f => f.FileType) .First(f => f.FilePath == m.Groups["pathToFile"].Value); var file = new CoreModels.NoteFileContent(noteId.ToString(), "file", dbFile.FilePath, dbFile.FileType.Type, dbFile.Id.ToString()); contents.Add(file); contents.Add(new CoreModels.NoteTextContent(noteId.ToString(), "text", noCode[i].Substring(j, m.Groups["allstr"].Index - j))); j = m.Groups["allstr"].Index + m.Groups["allstr"].Length; } contents.Add(new CoreModels.NoteTextContent(noteId.ToString(), "text", noCode[i].Substring(j, noCode[i].Length - j))); if (i < code.Count) { contents.Add(new CoreModels.NoteTextContent(noteId.ToString(), "text", code[i])); } i++; } return(contents.ToArray()); }