Skip to content

Commit e1e95ad

Browse files
committed
create mock data API
1 parent 50cbc6f commit e1e95ad

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

Controllers/HomeController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,24 @@ public IActionResult Index()
1313
{
1414
return View();
1515
}
16+
17+
[Route("initialMessages")]
18+
public JsonResult initialMessages(){
19+
var initialMessages = FakeMessageStore.FakeMessages.OrderByDescending(m => m.Date)
20+
.Take(2);
21+
22+
var initialValues = new ClientState(){
23+
Messages = initialMessages,
24+
LastFetchedMessageDate = initialMessages.Last().Date
25+
};
26+
27+
return Json(initialValues);
28+
}
29+
30+
[Route("fetchMessages")]
31+
public JsonResult FetchMessages(DateTime lastFetchedMessageDate){
32+
return Json(FakeMessageStore.FakeMessages.OrderByDescending(m => m.Date)
33+
.SkipWhile(m => m.Date >= lastFetchedMessageDate).Take(1));
34+
}
1635
}
1736
}

Models/ClientState.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
public class ClientState{
5+
[JsonProperty(PropertyName = "messages")]
6+
public IEnumerable<Message> Messages {get; set;}
7+
8+
[JsonProperty(PropertyName = "lastFetchedMessageDate")]
9+
public DateTime LastFetchedMessageDate {get; set;}
10+
}

Models/FakeMessageStore.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using System;
3+
4+
public class FakeMessageStore {
5+
private static DateTime startDateTime = new DateTime(2017, 11, 24);
6+
7+
public static readonly List<Message> FakeMessages = new List<Message>(){
8+
Message.CreateMessage("First message title", "First message text", startDateTime),
9+
Message.CreateMessage("2nd msg title", "2nd msg txt", startDateTime.AddDays(1)),
10+
Message.CreateMessage("3rd msg title", "3rd msg txt", startDateTime.AddDays(2)),
11+
Message.CreateMessage("4th msg title", "4th msg txt", startDateTime.AddDays(3)),
12+
Message.CreateMessage("5th msg title", "5th msg txt", startDateTime.AddDays(4)),
13+
Message.CreateMessage("6th msg title", "6th msg txy", startDateTime.AddDays(5))
14+
};
15+
}

Models/Message.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
public class Message{
4+
[JsonProperty(PropertyName="date")]
5+
public DateTime Date {get; set;}
6+
7+
[JsonProperty(PropertyName="title")]
8+
public string Title {get; set;}
9+
10+
[JsonProperty(PropertyName="text")]
11+
public string Text {get; set;}
12+
13+
private Message() {}
14+
15+
public static Message CreateMessage(string title, string text, DateTime date){
16+
return new Message(){
17+
Title = title,
18+
Text = text,
19+
Date = date
20+
};
21+
}
22+
}

0 commit comments

Comments
 (0)