Skip to content

Commit c5fdada

Browse files
author
Frode Sjovatsen
committed
added the rest of the classes from the java project
1 parent 8531d09 commit c5fdada

4 files changed

Lines changed: 138 additions & 12 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.Serialization;
3+
using Newtonsoft.Json;
4+
5+
namespace FINT.Model.Resource
6+
{
7+
public abstract class AbstractCollectionResources<T>
8+
{
9+
[JsonProperty(PropertyName = "_links")]
10+
protected readonly Dictionary<string, List<Link>> Links = new Dictionary<string, List<Link>>();
11+
12+
[JsonProperty(PropertyName = "_embedded")]
13+
protected EmbeddedResources<T> Embedded = new EmbeddedResources<T>();
14+
15+
[JsonProperty(PropertyName = "total_items")]
16+
protected int TotalItems { get; set; }
17+
18+
19+
[OnSerialized]
20+
internal void OnSerializedMethod(StreamingContext context)
21+
{
22+
TotalItems = Embedded.Entries.Count;
23+
}
24+
25+
public void AddResource(T resource)
26+
{
27+
Embedded.Entries.Add(resource);
28+
}
29+
30+
public List<Link> GetSelfLinks()
31+
{
32+
return Links["self"];
33+
}
34+
35+
/*
36+
@JsonIgnore
37+
public List<T> getContent() {
38+
return new ObjectMapper().convertValue(embedded.entries, getTypeReference());
39+
}
40+
41+
@JsonIgnore
42+
public abstract TypeReference<List<T>> getTypeReference();
43+
*/
44+
45+
public List<T> GetContent()
46+
{
47+
return Embedded.Entries;
48+
}
49+
50+
51+
public class EmbeddedResources<T>
52+
{
53+
public EmbeddedResources()
54+
{
55+
Entries = new List<T>();
56+
}
57+
58+
[JsonProperty(PropertyName = "_entries")]
59+
public List<T> Entries { get; set; }
60+
}
61+
}
62+
}
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
5-
</PropertyGroup>
6-
7-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>netstandard2.0</TargetFramework>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
7+
<HintPath>..\..\..\..\.nuget\packages\newtonsoft.json\9.0.1\lib\netstandard1.0\Newtonsoft.Json.dll</HintPath>
8+
</Reference>
9+
</ItemGroup>
10+
</Project>

FINT.Model.Resource/IFintLinks.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Collections.Generic;
2+
using System.Runtime.Serialization;
3+
using Newtonsoft.Json;
4+
5+
namespace FINT.Model.Resource
6+
{
7+
// TODO: Don't know how to do this since C# is not supporting default implementations
8+
public interface IFintLinks
9+
{
10+
Dictionary<string, List<Link>> GetLinks();
11+
12+
[JsonProperty(PropertyName = "_links")]
13+
Dictionary<string, List<Link>> Links { get; set; }
14+
15+
[OnSerialized]
16+
void OnSerializedMethod(StreamingContext context)
17+
{
18+
if (GetLinks().Count == 0)
19+
{
20+
Links = null;
21+
}
22+
23+
Links = GetLinks();
24+
}
25+
26+
[OnDeserialized]
27+
void OnDeserializedMethod(StreamingContext context)
28+
{
29+
if (Links == null) return;
30+
31+
foreach (var link in Links)
32+
{
33+
GetLinks().Add(link.Key, link.Value);
34+
}
35+
}
36+
37+
void AddLink(string key, Link link)
38+
{
39+
if (GetLinks().ContainsKey(key)) return;
40+
41+
GetLinks().Add(key, new List<Link>());
42+
GetLinks()[key].Add(link);
43+
}
44+
45+
Dictionary<string, List<Link>> CreateLinks()
46+
{
47+
return new Dictionary<string, List<Link>>();
48+
}
49+
50+
List<IFintLinks> GetNestedResources()
51+
{
52+
return new List<IFintLinks>();
53+
}
54+
55+
List<Link> GetSelfLinks()
56+
{
57+
return GetLinks()["self"];
58+
}
59+
60+
61+
}
62+
}

FINT.Model.Resource/Link.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,27 @@ public static Link with(string verdi)
2525

2626
public static Link with(Type placeholderClass, string[] pathElements)
2727
{
28-
return with(placeholderClass, String.Join("/", pathElements));
28+
return with(placeholderClass, string.Join("/", pathElements));
2929
}
3030

3131
private static Link with(Type placeholderClass, string path)
3232
{
33-
var placeholder = Link.getHrefPlaceholder(placeholderClass);
33+
var placeholder = getHrefPlaceholder(placeholderClass);
3434
var regex = new Regex("^/");
3535
path = regex.Replace(path, "", 1);
36-
36+
3737
return new Link(string.Format("${{{0}}}/{1}", placeholder, path));
3838
}
3939

4040
private static string getHrefPlaceholder(Type placeholderClass)
4141
{
4242
var regex1 = new Regex("^FINT\\.Model(\\.Resource)?\\.");
4343
var regex2 = new Regex("Resource$");
44-
44+
4545
var replace1 = regex1.Replace(placeholderClass.FullName, "", 1);
4646
var replace2 = regex2.Replace(replace1, "", 1);
4747

4848
return replace2.ToLower();
4949
}
50-
5150
}
5251
}

0 commit comments

Comments
 (0)