Skip to content

Commit 19f193d

Browse files
committed
Improved FileDoc
1 parent 9c1f86f commit 19f193d

File tree

6 files changed

+34
-58
lines changed

6 files changed

+34
-58
lines changed

PostTripletex/Authentication.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.IO;
32
using System.Text;
43
using System.Threading.Tasks;
54
using PostTripletex.Model;
@@ -22,7 +21,7 @@ public static async Task Authenticate()
2221

2322
await CreateSessionToken(new Credentials(tokens[0], tokens[1]));
2423

25-
File.WriteAllLines(Path.Combine("Data", "Tokens.txt"), tokens);
24+
FileDoc.WriteFile(tokens, "Tokens.txt");
2625

2726
break;
2827
}

PostTripletex/Command.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public static void Welcome()
2020
Console.WriteLine(" cu(customer) - Post/Del");
2121
Console.WriteLine(" co(contact) - Post");
2222
Console.WriteLine(" e(employee) - Post");
23-
Console.WriteLine("\nCommand: <operation> <quantity> <entity>");
24-
Console.WriteLine("Example: del 5 p (deletes 5 products)");
23+
Console.WriteLine("\n Examples:");
24+
Console.WriteLine(" <operation> <quantity> <entity>");
25+
Console.WriteLine(" del 5 p (deletes 5 products)");
2526
Console.Write("\n> ");
2627
}
2728

PostTripletex/Delete.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ public static async Task Product(int number)
3131
Console.Write($"\r{productNumber.Length - i} Product deleted");
3232
}
3333

34-
FileDoc.DeleteFile("Product.csv");
35-
36-
if (productNumber.Length - number > 0) FileDoc.WriteFile(readFile.Take(readFile.Length - number).ToArray(), "Product.csv");
34+
if (productNumber.Length - number > 0) FileDoc.WriteFile(readFile.Take(readFile.Length - number).ToArray(), "Product.csv", "Number,Name,Id");
3735

3836
Console.WriteLine("\n");
3937
Console.Write("> ");
@@ -63,9 +61,7 @@ public static async Task Customer(int number)
6361
Console.Write($"\r{productNumber.Length - i} Customer deleted");
6462
}
6563

66-
FileDoc.DeleteFile("Customer.csv");
67-
68-
if (productNumber.Length - number > 0) FileDoc.WriteFile(readFile.Take(readFile.Length - number).ToArray(), "Customer.csv");
64+
if (productNumber.Length - number > 0) FileDoc.WriteFile(readFile.Take(readFile.Length - number).ToArray(), "Customer.csv", "Name,Id");
6965

7066
Console.WriteLine("\n");
7167
Console.Write("> ");

PostTripletex/FileDoc.cs

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,44 @@
22
using System.IO;
33
using System.Linq;
44
using System.Threading.Tasks;
5-
using PostTripletex.Model;
65

76
namespace PostTripletex
87
{
98
public class FileDoc
109
{
11-
private const string _directory = "Data";
10+
private static readonly string _directory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\PostTripletex";
1211

13-
public static void WriteFile(KeyInfo info, string fileName)
14-
{
15-
var filePath = Path.Combine(_directory, fileName);
16-
17-
if (!File.Exists(filePath))
18-
{
19-
Directory.CreateDirectory(_directory);
20-
21-
using var swr = File.CreateText(filePath);
22-
swr.WriteLine(fileName == "Product.csv" ? "Number,Name,Id" : "Name,Id");
23-
}
24-
25-
using var sw = File.AppendText(filePath);
26-
sw.WriteLine(fileName == "Product.csv" ? $"{info.number},{info.name},{info.id}" : $"{info.name},{info.id}");
27-
}
28-
29-
public static void WriteFile(string[] info, string fileName)
12+
public static void WriteFile(string[] info, string fileName, string startLine = "")
3013
{
3114
var filePath = Path.Combine(_directory, fileName);
15+
16+
if (!Directory.Exists(_directory)) Directory.CreateDirectory(_directory);
3217

33-
if (!File.Exists(filePath))
18+
if (!string.IsNullOrEmpty(startLine))
3419
{
35-
Directory.CreateDirectory(_directory);
36-
37-
using var swr = File.CreateText(filePath);
38-
swr.WriteLine(fileName == "Product.csv" ? "Number,Name,Id" : "Name,Id");
20+
File.WriteAllText(filePath, startLine + "\n");
21+
File.AppendAllLines(filePath, info);
3922
}
40-
41-
foreach (var stg in info)
23+
else
4224
{
43-
var strings = stg.Split(',');
44-
45-
using var sw = File.AppendText(filePath);
46-
sw.WriteLine(fileName == "Product.csv" ? $"{strings[0]},{strings[1]},{strings[2]}" : $"{strings[0]},{strings[1]}");
25+
File.WriteAllLines(filePath, info);
4726
}
4827
}
4928

50-
public static string[] ReadFile(string fileName)
29+
public static void AppendFile(string info, string fileName)
5130
{
5231
var filePath = Path.Combine(_directory, fileName);
5332

54-
return File.Exists(filePath) ? File.ReadAllLines(filePath).Skip(1).ToArray() : null;
33+
if (!Directory.Exists(_directory)) Directory.CreateDirectory(_directory);
34+
35+
File.AppendAllText(filePath, info + "\n");
5536
}
5637

57-
public static void DeleteFile(string fileName)
38+
public static string[] ReadFile(string fileName)
5839
{
5940
var filePath = Path.Combine(_directory, fileName);
6041

61-
if (File.Exists(filePath)) File.Delete(filePath);
42+
return File.Exists(filePath) ? File.ReadAllLines(filePath).Skip(1).ToArray() : null;
6243
}
6344

6445
public static string GetNumber(string fileName)
@@ -68,9 +49,8 @@ public static string GetNumber(string fileName)
6849
if (!File.Exists(filePath)) return "0000";
6950

7051
var lastLine = File.ReadLines(filePath).Last().Split(',')[0];
71-
var i = (int.Parse(lastLine) + 1).ToString().PadLeft(4, '0');
7252

73-
return i;
53+
return int.TryParse(lastLine, out var numb) ? (numb + 1).ToString().PadLeft(4, '0') : "0000";
7454
}
7555

7656
public static async Task<string[]> GetTokens()

PostTripletex/Get.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
2-
using System.Linq;
1+
using System.Linq;
32
using System.Threading.Tasks;
4-
using Newtonsoft.Json;
53
using PostTripletex.Model;
64
using RestSharp;
75

@@ -28,9 +26,7 @@ public static async Task Product()
2826

2927
var data = response.Data.Values.Select(d => $"{d.number},{d.name},{d.id}").ToArray();
3028

31-
FileDoc.DeleteFile("Product.csv");
32-
33-
FileDoc.WriteFile(data, "Product.csv");
29+
FileDoc.WriteFile(data, "Product.csv", "Number,Name,Id");
3430
}
3531

3632
public static async Task Customer()
@@ -46,9 +42,7 @@ public static async Task Customer()
4642

4743
var data = response.Data.Values.Select(d => $"{d.name},{d.id}").ToArray();
4844

49-
FileDoc.DeleteFile("Customer.csv");
50-
51-
FileDoc.WriteFile(data, "Customer.csv");
45+
FileDoc.WriteFile(data, "Customer.csv", "Name,Id");
5246
}
5347

5448
public static async Task<long[]> Subscription()

PostTripletex/Post.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static async Task Contact(int number)
4545

4646
var response = await client.ExecutePostAsync<SingleValueResponse<KeyInfo>>(request);
4747

48-
if (HttpStatusCode.Created != response.StatusCode) throw new ArgumentException($"Contact post error: {response.StatusCode}");
48+
if (!response.IsSuccessful) ErrorHandler.Handel(response.Content);
4949

5050
Console.Write($"\r{i + 1} Contact created");
5151
}
@@ -87,7 +87,10 @@ public static async Task Product(int number)
8787

8888
if (!response.IsSuccessful) ErrorHandler.Handel(response.Content);
8989

90-
FileDoc.WriteFile(response.Data.Value, "Product.csv");
90+
var data = response.Data.Value;
91+
var dataString = $"{data.number},{data.name},{data.id}";
92+
93+
FileDoc.AppendFile(dataString, "Product.csv");
9194

9295
Console.Write($"\r{i + 1} Product created");
9396
}
@@ -152,7 +155,10 @@ public static async Task Customer(int number)
152155

153156
if (!response.IsSuccessful) ErrorHandler.Handel(response.Content);
154157

155-
FileDoc.WriteFile(response.Data.Value, "Customer.csv");
158+
var data = response.Data.Value;
159+
var dataString = $"{data.name},{data.id}";
160+
161+
FileDoc.AppendFile(dataString, "Customer.csv");
156162

157163
Console.Write($"\r{i + 1} Customer created");
158164
}

0 commit comments

Comments
 (0)