Advertisement
shady_obeyd

DataMapper.cs

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