Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using Models;
- using Newtonsoft.Json.Linq;
- namespace Repositories
- {
- public class JsonRepository
- {
- public IEnumerable<string> GetAllData(string source)
- {
- var files = Directory.GetFiles(source, "*.json", SearchOption.AllDirectories);
- List<string> datas = new List<string>();
- foreach (var file in files)
- {
- string data = File.ReadAllText(file);
- datas.Add(data);
- }
- return datas;
- }
- public IEnumerable<Meal> ParseJsonToMealObject(string data)
- {
- var jObj = JObject.Parse(data);
- var dayTime = jObj["calendar"]["dateToDayId"]
- .Select(x => (JProperty) x)
- .Select(p => new KeyValuePair<string, string>(p.Value.ToString(), p.Name.ToString()));
- Dictionary<string, string> myDict = dayTime.ToDictionary(x => x.Key, x => x.Value);
- var meals = jObj["calendar"]["daysWithDetails"]
- .Select(x => (JProperty) x)
- .SelectMany(p => (JToken) p.Value["details"]["mealsWithDetails"])
- .SelectMany(m => (JProperty) m)
- .Select(q => q["meal"])
- .Select(w => new Meal()
- {
- userId = w["userId"].ToString(),
- day = myDict[w["dayId"].ToString()]
- });
- return meals;
- }
- public Dictionary<string, int> GetMealPerUser(IEnumerable<Meal> meals, string startDate, string endDate)
- {
- DateTime startDateTime = DateTime.Parse(startDate);
- DateTime endDateTime = DateTime.Parse(endDate);
- Dictionary<string, int> mealCount = new Dictionary<string, int>();
- //Console.WriteLine(mealCount[10]);
- foreach (var s in meals)
- {
- DateTime tmpDate = DateTime.Parse(s.day);
- if (tmpDate >= startDateTime && tmpDate <= endDateTime)
- {
- if (!mealCount.ContainsKey(s.userId))
- {
- mealCount.Add(s.userId, 0);
- }
- mealCount[s.userId]++;
- }
- // Console.WriteLine(s.day + " " + s.userId);
- }
- return mealCount;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement