Advertisement
Guest User

Untitled

a guest
Mar 9th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.82 KB | None | 0 0
  1. using Forum.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. public class DataMapper
  9. {
  10.     private const string DATA_PATH = "../data/";
  11.     private const string CONFIG_PATH = "config.ini";
  12.     private const string DEFAULT_CONFIG = "users=users.csv\r\ncategories=categories.csv\r\nposts=posts.csv\r\nreplies=replies.csv";
  13.  
  14.     private static readonly Dictionary<string, string> config;
  15.  
  16.     static DataMapper()
  17.     {
  18.         Directory.CreateDirectory(DATA_PATH);
  19.         config = LoadConfig(DATA_PATH + CONFIG_PATH);
  20.     }
  21.  
  22.  
  23.  
  24.     private static void EnsureConfigFile(string configPath)
  25.     {
  26.         if (!File.Exists(configPath))
  27.         {
  28.             File.WriteAllText(configPath, DEFAULT_CONFIG);
  29.         }
  30.     }
  31.  
  32.     private static void EnsureFile(string path)
  33.     {
  34.         if (!File.Exists(path))
  35.         {
  36.             File.Create(path).Close();
  37.         }
  38.     }
  39.  
  40.     private static Dictionary<string,string> LoadConfig(string configPath)
  41.     {
  42.         EnsureConfigFile(configPath);
  43.  
  44.         var contents = ReadLines(configPath);
  45.         var config = contents.Select(l => l.Split('=')).ToDictionary(t => t[0], t => DATA_PATH + t[1]);
  46.         return config;
  47.     }
  48.  
  49.     private static string[] ReadLines(string path)
  50.     {
  51.         EnsureFile(path);
  52.  
  53.         var lines = File.ReadAllLines(path);
  54.         return lines;
  55.     }
  56.  
  57.     private static void WriteLines(string path, string[] lines)
  58.     {
  59.         File.WriteAllLines(path, lines);
  60.     }
  61.  
  62.     public static List<Category> LoadCategories()
  63.     {
  64.         List<Category> categories = new List<Category>();
  65.         var dataLines = ReadLines(config["categories"]);
  66.  
  67.         foreach (var line in dataLines)
  68.         {
  69.             var args = line.Split(",");
  70.             var id = int.Parse(args[0]);
  71.             var name = args[1];
  72.             var postIds = args[2].Split(',', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  73.             Category category = new Category(id, name, postIds);
  74.             categories.Add(category);
  75.         }
  76.         return categories;
  77.     }
  78.  
  79.     public static void SaveCategories(List<Category> categories)
  80.     {
  81.         List<string> lines = new List<string>();
  82.  
  83.         foreach (var category in categories)
  84.         {
  85.             const string categoryFormat = "{0},{1},{2}";
  86.             string line = string.Format(categoryFormat, category.Id, category.Name, string.Join(",", category.Posts));
  87.             lines.Add(line);
  88.         }
  89.         WriteLines(config["categories"], lines.ToArray());
  90.     }
  91.  
  92.     public static List<User> LoadUsers()
  93.     {
  94.         List<User> users = new List<User>();
  95.         var dataLines = ReadLines(config["users"]);
  96.  
  97.         foreach (var line in dataLines)
  98.         {
  99.             var args = line.Split(",");
  100.             var id = int.Parse(args[0]);
  101.             var userName = args[1];
  102.             var password = args[2];
  103.             var postIds = args[3].Split(',', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  104.             User user = new User(id, userName,password, postIds);
  105.             users.Add(user);
  106.         }
  107.         return users;
  108.     }
  109.     public static void SaveUsers(List<User> users)
  110.     {
  111.         List<string> lines = new List<string>();
  112.  
  113.         foreach (var user in users)
  114.         {
  115.             const string categoryFormat = "{0},{1},{2},{3}";
  116.             string line = string.Format(categoryFormat, user.Id, user.Username,user.Password, string.Join(",", user.PostIds));
  117.             lines.Add(line);
  118.         }
  119.         WriteLines(config["users"], lines.ToArray());
  120.     }
  121.  
  122.     public static List<Post> LoadPosts()
  123.     {
  124.         List<Post> posts = new List<Post>();
  125.         var dataLines = ReadLines(config["posts"]);
  126.  
  127.         foreach (var line in dataLines)
  128.         {
  129.             var args = line.Split(",");
  130.             var id = int.Parse(args[0]);
  131.             var title = args[1];
  132.             var content = args[2];
  133.             var categoryId = int.Parse(args[3]);
  134.             var authorId = int.Parse(args[4]);
  135.             var replies = args[5].Split(',', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  136.             Post post = new Post(id, title, content, categoryId,authorId,replies);
  137.             posts.Add(post);
  138.         }
  139.         return posts;
  140.     }
  141.  
  142.     public static void SavePosts(List<Post> posts)
  143.     {
  144.         List<string> lines = new List<string>();
  145.  
  146.         foreach (var post in posts)
  147.         {
  148.             const string categoryFormat = "{0},{1},{2},{3},{4},{5}";
  149.             string line = string.Format(categoryFormat, post.Id, post.Title, post.Content,post.CategoryId,post.AuthorId, string.Join(",", post.Replies));
  150.             lines.Add(line);
  151.         }
  152.         WriteLines(config["posts"], lines.ToArray());
  153.     }
  154.  
  155.     public static List<Reply> LoadReplies()
  156.     {
  157.         List<Reply> replies = new List<Reply>();
  158.         var dataLines = ReadLines(config["replies"]);
  159.  
  160.         foreach (var line in dataLines)
  161.         {
  162.             var args = line.Split(",");
  163.             var id = int.Parse(args[0]);
  164.             var content = args[1];
  165.            
  166.             var authorId = int.Parse(args[3]);
  167.             var postId = int.Parse(args[4]);
  168.             Reply reply = new Reply(id,content, authorId, postId);
  169.             replies.Add(reply);
  170.         }
  171.         return replies;
  172.     }
  173.  
  174.     public static void SaveReplies(List<Reply> replies)
  175.     {
  176.         List<string> lines = new List<string>();
  177.  
  178.         foreach (var reply in replies)
  179.         {
  180.             const string categoryFormat = "{0},{1},{2},{3}";
  181.             string line = string.Format(categoryFormat, reply.Id, reply.Content, reply.AuthorId,reply.PostId);
  182.             lines.Add(line);
  183.         }
  184.         WriteLines(config["replies"], lines.ToArray());
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement