Skip to content

Commit 43c0d2f

Browse files
committed
change report to parsing column name
1 parent b54e5fe commit 43c0d2f

File tree

6 files changed

+548
-25
lines changed

6 files changed

+548
-25
lines changed

Source/FikaAmazonAPI.SampleCode/Program.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ static async Task Main(string[] args)
3232
IsActiveLimitRate = true
3333
});
3434

35-
36-
3735
ReportManager reportManager2 = new ReportManager(amazonConnection);
38-
var products2 = await reportManager2.GetProductsAsync(); //GET_MERCHANT_LISTINGS_ALL_DATA
36+
var products2 = await reportManager2.GetReturnFBAOrderAsync(3); //GET_MERCHANT_LISTINGS_ALL_DATA
3937

4038

4139
ParameterOrderList serachOrderList = new ParameterOrderList();
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace FikaAmazonAPI.ReportGeneration.ReportDataTable
6+
{
7+
public static class RowExtensionMethods
8+
{
9+
public static string GetString(this TableRow row, string id)
10+
{
11+
return AValueWithThisIdExists(row, id)
12+
? row[id]
13+
: null;
14+
}
15+
public static int GetInt32(this TableRow row, string id)
16+
{
17+
return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id)
18+
? Convert.ToInt32(row[id])
19+
: int.MinValue;
20+
}
21+
public static long GetInt64(this TableRow row, string id)
22+
{
23+
return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id)
24+
? Convert.ToInt64(row[id])
25+
: long.MinValue;
26+
}
27+
public static decimal GetDecimal(this TableRow row, string id)
28+
{
29+
return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id)
30+
? Convert.ToDecimal(row[id])
31+
: decimal.MinValue;
32+
}
33+
public static DateTime GetDateTime(this TableRow row, string id)
34+
{
35+
return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id)
36+
? Convert.ToDateTime(row[id])
37+
: DateTime.MinValue;
38+
}
39+
public static bool GetBoolean(this TableRow row, string id)
40+
{
41+
if (TheBooleanValueIsEmpty(row, id))
42+
return false;
43+
44+
AssertThatTheRequestIsValid(row, id);
45+
46+
return string.Equals(row[id], "true", StringComparison.OrdinalIgnoreCase);
47+
}
48+
private static void AssertThatTheRequestIsValid(TableRow row, string id)
49+
{
50+
AssertThatAValueWithThisIdExistsInThisRow(row, id);
51+
AssertThatThisIsAnAcceptableBoolValue(row, id);
52+
}
53+
private static void AssertThatThisIsAnAcceptableBoolValue(TableRow row, string id)
54+
{
55+
var acceptedValues = new[] { "true", "false" };
56+
if (acceptedValues.Contains(row[id], StringComparer.OrdinalIgnoreCase) == false)
57+
throw new InvalidCastException($"You must use 'true' or 'false' when setting bools for {id}");
58+
}
59+
60+
private static void AssertThatAValueWithThisIdExistsInThisRow(TableRow row, string id)
61+
{
62+
if (AValueWithThisIdExists(row, id) == false)
63+
throw new InvalidOperationException($"{id} could not be found in the row.");
64+
}
65+
private static bool TheBooleanValueIsEmpty(TableRow row, string id)
66+
{
67+
return AValueWithThisIdExists(row, id) && string.IsNullOrEmpty(row[id]);
68+
}
69+
70+
public static float GetSingle(this TableRow row, string id)
71+
{
72+
return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id)
73+
? Convert.ToSingle(row[id])
74+
: float.MinValue;
75+
}
76+
public static char GetChar(this TableRow row, string id)
77+
{
78+
return Convert.ToChar(row[id]);
79+
}
80+
public static T GetDiscreteEnum<T>(this TableRow row, string id) where T : struct, IConvertible
81+
{
82+
var value = row[id].Replace(" ", string.Empty);
83+
T @enum;
84+
if (Enum.TryParse(value, true, out @enum))
85+
return @enum;
86+
87+
throw new InvalidOperationException($"No enum with value {value} found in enum {typeof(T).Name}");
88+
}
89+
public static T GetDiscreteEnum<T>(this TableRow row, string id, T defaultValue) where T : struct, IConvertible
90+
{
91+
var value = row[id].Replace(" ", string.Empty);
92+
T @enum;
93+
return Enum.TryParse(value, true, out @enum) ? @enum : defaultValue;
94+
}
95+
public static TEnum GetEnumValue<TEnum>(this TableRow row, string id)
96+
{
97+
return (TEnum)Enum.Parse(typeof(TEnum), row[id]);
98+
}
99+
public static Enum GetEnum<T>(this TableRow row, string id) where T : class
100+
{
101+
return GetTheEnumValue<T>(row[id], id);
102+
}
103+
private static Enum GetTheEnumValue<T>(string rowValue, string propertyName) where T : class
104+
{
105+
var value = rowValue.Replace(" ", string.Empty);
106+
107+
var enumType = GetTheEnumType<T>(propertyName, value);
108+
109+
return Enum.Parse(enumType, value, true) as Enum;
110+
}
111+
112+
private static Type GetTheEnumType<T>(string propertyName, string value)
113+
{
114+
var propertyType = (from property in typeof(T).GetProperties()
115+
where property.Name == propertyName
116+
&& property.PropertyType.IsEnum
117+
&& EnumValueIsDefinedCaseInsensitve(property.PropertyType, value)
118+
select property.PropertyType).FirstOrDefault();
119+
120+
if (propertyType == null)
121+
throw new InvalidOperationException($"No enum with value {value} found in type {typeof(T).Name}");
122+
123+
return propertyType;
124+
}
125+
private static bool EnumValueIsDefinedCaseInsensitve(Type @enum, string value)
126+
{
127+
Enum parsedEnum = null;
128+
try
129+
{
130+
parsedEnum = Enum.Parse(@enum, value, true) as Enum;
131+
}
132+
catch
133+
{
134+
// just catch it
135+
}
136+
137+
return parsedEnum != null;
138+
}
139+
public static Guid GetGuid(this TableRow row, string id)
140+
{
141+
return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id)
142+
? new Guid(row[id])
143+
: new Guid();
144+
}
145+
public static double GetDouble(this TableRow row, string id)
146+
{
147+
return AValueWithThisIdExists(row, id) && TheValueIsNotEmpty(row, id)
148+
? Convert.ToDouble(row[id])
149+
: double.MinValue;
150+
}
151+
private static bool AValueWithThisIdExists(IEnumerable<KeyValuePair<string, string>> row, string id)
152+
{
153+
return row.Any(x => x.Key == id);
154+
}
155+
private static bool TheValueIsNotEmpty(TableRow row, string id)
156+
{
157+
return string.IsNullOrEmpty(row[id]) == false;
158+
}
159+
}
160+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace FikaAmazonAPI.ReportGeneration.ReportDataTable
8+
{
9+
public class Table
10+
{
11+
internal const string ERROR_NO_CELLS_TO_ADD = "No cells to add";
12+
internal const string ERROR_NO_HEADER_TO_ADD = "No headers to add";
13+
internal const string ERROR_COLUMN_NAME_NOT_FOUND = "Could not find a column named '{0}' in the table.";
14+
internal const string ERROR_CELLS_NOT_MATCHING_HEADERS = "The number of cells ({0}) you are trying to add doesn't match the number of columns ({1})";
15+
16+
private readonly string[] header;
17+
private readonly TableRows rows = new TableRows();
18+
19+
public ICollection<string> Header
20+
{
21+
get { return header; }
22+
}
23+
24+
public TableRows Rows
25+
{
26+
get { return rows; }
27+
}
28+
29+
public int RowCount
30+
{
31+
get { return rows.Count; }
32+
}
33+
34+
public Table(params string[] header)
35+
{
36+
37+
if (header == null || header.Length == 0)
38+
{
39+
throw new ArgumentException(ERROR_NO_HEADER_TO_ADD, "header");
40+
}
41+
for (int colIndex = 0; colIndex < header.Length; colIndex++)
42+
header[colIndex] = header[colIndex] ?? string.Empty;
43+
this.header = header;
44+
}
45+
46+
public static Table ConvertFromCSV(string path, char separator = '\t')
47+
{
48+
var lines = File.ReadAllLines(path);
49+
50+
var table = new Table(lines.First().Split(separator));
51+
52+
53+
lines.Skip(1).ToList().ForEach(a => ConvertFromCSVAddRow(table, a, separator));
54+
return table;
55+
}
56+
private static void ConvertFromCSVAddRow(Table table, string line, char separator)
57+
{
58+
table.AddRow(line.Split(separator));
59+
}
60+
public bool ContainsColumn(string column)
61+
{
62+
return GetHeaderIndex(column, false) >= 0;
63+
}
64+
65+
internal int GetHeaderIndex(string column, bool throwIfNotFound = true)
66+
{
67+
int index = Array.IndexOf(header, column);
68+
if (!throwIfNotFound)
69+
return index;
70+
if (index < 0)
71+
{
72+
var mess = string.Format(
73+
ERROR_COLUMN_NAME_NOT_FOUND + "\nThe table looks like this:\n{1}",
74+
column,
75+
this);
76+
throw new IndexOutOfRangeException(mess);
77+
}
78+
return index;
79+
}
80+
81+
public void AddRow(IDictionary<string, string> values)
82+
{
83+
string[] cells = new string[header.Length];
84+
foreach (var value in values)
85+
{
86+
int headerIndex = GetHeaderIndex(value.Key);
87+
cells[headerIndex] = value.Value;
88+
}
89+
90+
AddRow(cells);
91+
}
92+
93+
public void AddRow(params string[] cells)
94+
{
95+
if (cells == null)
96+
throw new Exception(ERROR_NO_CELLS_TO_ADD);
97+
98+
if (cells.Length != header.Length)
99+
{
100+
var mess =
101+
string.Format(
102+
ERROR_CELLS_NOT_MATCHING_HEADERS + ".\nThe table looks like this\n{2}",
103+
cells.Length,
104+
header.Length,
105+
this);
106+
throw new ArgumentException(mess);
107+
}
108+
var row = new TableRow(this, cells);
109+
rows.Add(row);
110+
}
111+
112+
public void RenameColumn(string oldColumn, string newColumn)
113+
{
114+
int colIndex = GetHeaderIndex(oldColumn);
115+
header[colIndex] = newColumn;
116+
}
117+
118+
public override string ToString()
119+
{
120+
return ToString(false, true);
121+
}
122+
123+
public string ToString(bool headersOnly = false, bool withNewline = true)
124+
{
125+
int[] columnWidths = new int[header.Length];
126+
for (int colIndex = 0; colIndex < header.Length; colIndex++)
127+
columnWidths[colIndex] = header[colIndex].Length;
128+
129+
if (!headersOnly)
130+
{
131+
foreach (TableRow row in rows)
132+
{
133+
for (int colIndex = 0; colIndex < header.Length; colIndex++)
134+
columnWidths[colIndex] = Math.Max(columnWidths[colIndex], row[colIndex].Length);
135+
}
136+
}
137+
138+
StringBuilder builder = new StringBuilder();
139+
AddTableRow(builder, header, columnWidths);
140+
141+
if (!headersOnly)
142+
{
143+
foreach (TableRow row in rows)
144+
AddTableRow(builder, row.Select(pair => pair.Value), columnWidths);
145+
}
146+
147+
if (!withNewline)
148+
{
149+
var newlineLength = Environment.NewLine.Length;
150+
builder.Remove(builder.Length - newlineLength, newlineLength);
151+
}
152+
153+
return builder.ToString();
154+
}
155+
156+
private void AddTableRow(StringBuilder builder, IEnumerable<string> cells, int[] widths)
157+
{
158+
const string margin = " ";
159+
const string separator = "|";
160+
int colIndex = 0;
161+
162+
builder.Append(separator);
163+
foreach (string cell in cells)
164+
{
165+
builder.Append(margin);
166+
167+
builder.Append(cell);
168+
builder.Append(' ', widths[colIndex] - cell.Length);
169+
170+
builder.Append(margin);
171+
builder.Append(separator);
172+
173+
colIndex++;
174+
}
175+
176+
builder.AppendLine();
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)