Advertisement
tamim_arefin_anik

sample

Nov 13th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Models;
  7. using Newtonsoft.Json.Linq;
  8.  
  9. namespace Repositories
  10. {
  11.     public class JsonRepository
  12.     {
  13.        
  14.         public IEnumerable<string> GetAllData(string source)
  15.         {
  16.             var files = Directory.GetFiles(source, "*.json", SearchOption.AllDirectories);
  17.            
  18.             List<string> datas = new List<string>();
  19.             foreach (var file in files)
  20.             {
  21.                 string data = File.ReadAllText(file);
  22.                 datas.Add(data);
  23.             }
  24.  
  25.             return datas;
  26.         }
  27.  
  28.         public IEnumerable<Meal> ParseJsonToMealObject(string data)
  29.         {
  30.             var jObj = JObject.Parse(data);
  31.  
  32.             var dayTime = jObj["calendar"]["dateToDayId"]
  33.                 .Select(x => (JProperty) x)
  34.                 .Select(p => new KeyValuePair<string, string>(p.Value.ToString(), p.Name.ToString()));
  35.  
  36.             Dictionary<string, string> myDict = dayTime.ToDictionary(x => x.Key, x => x.Value);
  37.                
  38.  
  39.             var meals = jObj["calendar"]["daysWithDetails"]
  40.                 .Select(x => (JProperty) x)
  41.                 .SelectMany(p => (JToken) p.Value["details"]["mealsWithDetails"])
  42.                 .SelectMany(m => (JProperty) m)
  43.                 .Select(q => q["meal"])
  44.                 .Select(w => new Meal()
  45.                 {
  46.                     userId = w["userId"].ToString(),
  47.                     day = myDict[w["dayId"].ToString()]
  48.                 });
  49.             return meals;
  50.         }
  51.  
  52.         public Dictionary<string, int> GetMealPerUser(IEnumerable<Meal> meals, string startDate, string endDate)
  53.         {
  54.             DateTime startDateTime = DateTime.Parse(startDate);
  55.             DateTime endDateTime = DateTime.Parse(endDate);
  56.            
  57.             Dictionary<string, int> mealCount = new Dictionary<string, int>();
  58.            
  59.             //Console.WriteLine(mealCount[10]);
  60.             foreach (var s in meals)
  61.             {
  62.                 DateTime tmpDate = DateTime.Parse(s.day);
  63.                 if (tmpDate >= startDateTime && tmpDate <= endDateTime)
  64.                 {
  65.                     if (!mealCount.ContainsKey(s.userId))
  66.                     {
  67.                         mealCount.Add(s.userId, 0);
  68.                     }
  69.  
  70.                     mealCount[s.userId]++;
  71.                 }
  72.                 // Console.WriteLine(s.day + "  " + s.userId);
  73.             }
  74.  
  75.             return mealCount;
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement