/UnifiAppSolution/UnifiApp/AddNewUserGroup.xaml.cs

https://bitbucket.org/koitkorvel/teliaprojectunifi · C# · 110 lines · 84 code · 20 blank · 6 comment · 3 complexity · 1c1a6fbbc25bafb892f89742c8566709 MD5 · raw file

  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web.Script.Serialization;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. namespace UnifiApp
  21. {
  22. /// <summary>
  23. /// Interaction logic for AddNewUserGroup.xaml
  24. /// </summary>
  25. public partial class AddNewUserGroup : Page
  26. {
  27. static List<JsonUserGroups> userGroupList = new List<JsonUserGroups>();
  28. const String randomStringGenerator = "abcdefghijklmnopqrstuvwxyz0123456789";
  29. private static Random random = new Random((int)DateTime.Now.Ticks);
  30. public AddNewUserGroup()
  31. {
  32. InitializeComponent();
  33. JsonListSitesTextbox.Visibility = Visibility.Hidden;
  34. String allSites = File.ReadAllText(@"D:\Kool\TeliaProject\teliaprojectunifi\UnifiAppSolution\UnifiApp\testDataJson\JsonSites.json");
  35. JsonListSitesTextbox.Text = allSites;
  36. JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
  37. List<JsonSites> correctSitesJsonList = (List<JsonSites>)javaScriptSerializer.Deserialize(JsonListSitesTextbox.Text, typeof(List<JsonSites>));
  38. List<JsonSites> sortedSitesJsonList = correctSitesJsonList.OrderBy(o => o.desc).ToList();
  39. foreach (JsonSites jsonSites in sortedSitesJsonList)
  40. {
  41. ListSitesUserGroupCombobox.Items.Add(jsonSites.desc);
  42. string SiteId = jsonSites.name;
  43. string Site_Id = jsonSites._id;
  44. }
  45. }
  46. private static string RandomStringGenerator(int length)
  47. {
  48. var randomBuilder = new StringBuilder();
  49. for (var i = 0; i < length; i++)
  50. {
  51. var c = randomStringGenerator[random.Next(0, randomStringGenerator.Length)];
  52. randomBuilder.Append(c);
  53. }
  54. return randomBuilder.ToString();
  55. }
  56. private void AddUserGroupButton_Click(object sender, RoutedEventArgs e)
  57. {
  58. JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
  59. List<JsonSites> correctSitesJsonList = (List<JsonSites>)javaScriptSerializer.Deserialize(JsonListSitesTextbox.Text, typeof(List<JsonSites>));
  60. List<JsonSites> sortedSitesJsonList = correctSitesJsonList.OrderBy(o => o.desc).ToList();
  61. string selectedSite = ListSitesUserGroupCombobox.SelectedItem.ToString();
  62. foreach (JsonSites sites in sortedSitesJsonList)
  63. {
  64. if(selectedSite == sites.desc)
  65. {
  66. var filePath = @"D:\Kool\TeliaProject\teliaprojectunifi\UnifiAppSolution\UnifiApp\testDataJson\JsonUserGroups.json";
  67. // Read existing json data
  68. userGroupList = JsonConvert.DeserializeObject<List<JsonUserGroups>>(System.IO.File.ReadAllText(filePath));
  69. JsonUserGroups newJsonUserGroups = new JsonUserGroups
  70. {
  71. _id = RandomStringGenerator(25),
  72. name = UserGroupNameTextBox.Text,
  73. attr_hidden_id = "Default",
  74. attr_no_delete = true,
  75. qos_rate_max_down = int.Parse(UserGroupDownloadTextBox.Text),
  76. qos_rate_max_up = int.Parse(UserGroupUploadTextBox.Text),
  77. site_id = sites._id,
  78. };
  79. userGroupList.Add(newJsonUserGroups);
  80. // Update json data string
  81. String json = JsonConvert.SerializeObject(userGroupList, Formatting.Indented);
  82. System.IO.File.WriteAllText(filePath, json);
  83. string correctJson = File.ReadAllText(filePath);
  84. //MessageBox.Show(correctJson);
  85. MessageBox.Show("Added");
  86. }
  87. }
  88. }
  89. }
  90. }