static async System.Threading.Tasks.Task Main(string[] args) { /*IList<User> listStudents = await CrudHandler.GetGenericArrayAsync<User>(UriHelper.UsersUri); * * foreach (var user in listStudents) * { * Console.WriteLine(user.ToString()); * } */ var userlogin = new UserDto() { Username = "******", Password = "******" }; //Convert UserDto Object to JSON data for use in a POST request var studentJson = JsonConvert.SerializeObject(userlogin); var studentStringContent = new StringContent(studentJson, Encoding.UTF8, "application/json"); User x = await CrudHandler.Login <User>(studentStringContent); if (userlogin.Username == x.Username) { Console.WriteLine("Login successful"); } var fn = Console.ReadLine(); }
/// <summary>Updates the user asynchronous.</summary> /// <param name="firstname">The firstname.</param> /// <param name="lastname">The lastname.</param> /// <param name="username">The username.</param> /// <param name="password">The password.</param> /// <returns></returns> internal async Task <bool> UpdateUserAsync(string firstname, string lastname, string username, string password) { var currentUser = System.Text.Json.JsonSerializer.Deserialize <User>(ReadSetting("CurrentUser")); var user = new UserDto() { FirstName = firstname, LastName = lastname, Username = username, Password = password }; var userJson = JsonConvert.SerializeObject(user); var userStringContent = new StringContent(userJson, Encoding.UTF8, "application/json"); if (await CrudHandler.PutGenericAsync(userStringContent, ReadSetting("AuthInfo"), new Uri("http://localhost:4000/Users/" + currentUser.Id)).ConfigureAwait(false)) { userStringContent.Dispose(); string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password)); SaveSetting("AuthInfo", svcCredentials); Uri UsersUri = new Uri("http://localhost:4000/Users/" + currentUser.Id); User x = await CrudHandler.GetGenericEntityAsync <User>(UsersUri, ReadSetting("AuthInfo")).ConfigureAwait(false); var CurrentUserJson = JsonConvert.SerializeObject(x); if (x.Username == username) { SaveSetting("CurrentUser", CurrentUserJson); } return(true); } else { userStringContent.Dispose(); } return(false); }
/// <summary>Logins the user asynchronous.</summary> /// <param name="username">The username.</param> /// <param name="password">The password.</param> /// <returns></returns> internal async Task <bool> LoginUserAsync(string username, string password) { var user = new UserDto() { Username = username, Password = password }; var userJson = JsonConvert.SerializeObject(user); var userStringContent = new StringContent(userJson, Encoding.UTF8, "application/json"); User x = await CrudHandler.Login <User>(userStringContent).ConfigureAwait(false); var currentUserJson = JsonConvert.SerializeObject(x); userStringContent.Dispose(); //var CurrentUserStringContent = new StringContent(CurrentUserJson, Encoding.UTF8, "application/json"); if (x.Username == username) { string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password)); SaveSetting("AuthInfo", svcCredentials); SaveSetting("CurrentUser", currentUserJson); return(true); } else { return(false); } }
/// <summary>Updates the collective asynchronous.</summary> /// <param name="name">The name.</param> /// <param name="description">The description.</param> /// <param name="size">The size.</param> /// <returns></returns> internal async Task <bool> UpdateCollectiveAsync(string name, string description, int size) { var currentUser = System.Text.Json.JsonSerializer.Deserialize <User>(ReadSetting("CurrentUser")); #pragma warning disable CA1305 // Specify IFormatProvider var collective = new Collective() { Id = Int32.Parse(ReadSetting("CurrentCollective")), OwnerId = currentUser.Id, Name = name, Description = description, Size = size }; #pragma warning restore CA1305 // Specify IFormatProvider Uri collectiveUri = new Uri("http://localhost:4000/Collectives/" + collective.Id); var collectiveJson = JsonConvert.SerializeObject(collective); #pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task Collective currentCollective = await CrudHandler.GetGenericEntityAsync <Collective>(collectiveUri, ReadSetting("AuthInfo")); #pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task var collectiveStringContent = new StringContent(collectiveJson, Encoding.UTF8, "application/json"); if (currentUser.Id == currentCollective.OwnerId) { await CrudHandler.PutGenericAsync(collectiveStringContent, ReadSetting("AuthInfo"), collectiveUri).ConfigureAwait(false); collectiveStringContent.Dispose(); return(true); } else { collectiveStringContent.Dispose(); await new MessageDialog("You are not allowed to edit this Collective, please pick one from the Manage Collectives Page", "Not allowed").ShowAsync(); return(false); } }
public void ZeroTeacherIdFailsValidation() { // Setup - as from controller var key = 0; // in parameter IResult result = CrudHandler.Get <TeacherDto>(key); Assert.AreEqual(result.ValidationIssues.Count(), 1); Assert.AreEqual(result.ValidationIssues.First(), "Key must be greater than zero"); }
/// <summary>Creates the collective asynchronous.</summary> /// <param name="name">The name.</param> /// <param name="description">The description.</param> /// <param name="size">The size.</param> internal async Task CreateCollectiveAsync(string name, string description, int size) { var currentUser = System.Text.Json.JsonSerializer.Deserialize <User>(ReadSetting("CurrentUser")); var collective = new Collective() { Name = name, Description = description, Size = size, OwnerId = currentUser.Id }; var collectiveJson = JsonConvert.SerializeObject(collective); var collectiveStringContent = new StringContent(collectiveJson, Encoding.UTF8, "application/json"); Collective x = await CrudHandler.CreateCollective <Collective>(collectiveStringContent, ReadSetting("AuthInfo")).ConfigureAwait(false); collectiveStringContent.Dispose(); await JoinCollectiveAsync(x.Id).ConfigureAwait(true); }
public void CanGetTeacherById() { // Setup - as from controller var key = 6; // in parameter IResult result = CrudHandler.Get <TeacherDto>(key); Assert.IsTrue(result.ValidationIssues.Count() == 0); Assert.IsTrue(result.Exceptions.Count() == 0); TeacherDto typedResult = result switch { Result <int, TeacherDto> x => x.Output, _ => default }; Assert.AreEqual(typedResult.FirstName, "Apple"); }
/// <summary>Deletes the post asynchronous.</summary> /// <returns></returns> internal async Task <bool> DeletePostAsync() { var currentUser = System.Text.Json.JsonSerializer.Deserialize <User>(ReadSetting("CurrentUser")); var post = GetCurrentPost(); if (currentUser.Id == post.OwnerId) { Uri postdelete = new Uri("http://localhost:4000/Posts/" + post.Id); await CrudHandler.DeleteGenericAsync(ReadSetting("AuthInfo"), postdelete).ConfigureAwait(true); return(true); } else { return(false); } }
/// <summary>Creates the post asynchronous.</summary> /// <param name="title">The title.</param> /// <param name="content">The content.</param> internal async Task CreatePostAsync(string title, string content) { var currentUser = System.Text.Json.JsonSerializer.Deserialize <User>(ReadSetting("CurrentUser")); var collectiveId = ReadSetting("CurrentCollective"); #pragma warning disable CA1305 // Specify IFormatProvider var post = new Post() { OwnerId = currentUser.Id, CollectiveId = Int32.Parse(collectiveId), Title = title, Content = content }; #pragma warning restore CA1305 // Specify IFormatProvider var postJson = JsonConvert.SerializeObject(post); var postStringContent = new StringContent(postJson, Encoding.UTF8, "application/json"); await CrudHandler.CreatePost(postStringContent, ReadSetting("AuthInfo")).ConfigureAwait(false); postStringContent.Dispose(); }
/// <summary>Deletes the collective asynchronous.</summary> internal async Task DeleteCollectiveAsync() { try { var currentUser = System.Text.Json.JsonSerializer.Deserialize <User>(ReadSetting("CurrentUser")); var collective = GetCurrentCollective(); if (currentUser.Id == collective.OwnerId) { Uri coldelete = new Uri("http://localhost:4000/Collectives/" + collective.Id); await CrudHandler.DeleteGenericAsync(ReadSetting("AuthInfo"), coldelete).ConfigureAwait(true); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary>Loads the users asynchronous.</summary> internal async Task LoadUsersAsync() { try { Users.Clear(); Uri usrUri = new Uri("http://localhost:4000/CollectiveUsers/RelevantUsers/" + ReadSetting("CurrentCollective")); IList <User> users = await CrudHandler.GetGenericArrayAsync <User>(usrUri, ReadSetting("AuthInfo")).ConfigureAwait(true); foreach (var item in users) { Users.Add(item); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary>Loads the posts asynchronous.</summary> internal async Task LoadPostsAsync() { try { Posts.Clear(); Uri postUri = new Uri("http://localhost:4000/Posts/RelevantPosts/" + ReadSetting("CurrentCollective")); IList <Post> postList = await CrudHandler.GetGenericArrayAsync <Post>(postUri, ReadSetting("AuthInfo")).ConfigureAwait(true); foreach (var item in postList) { Posts.Add(item); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary>Loads the collectives asynchronous.</summary> internal async Task LoadCollectivesAsync() { try { Collectives.Clear(); var currentUser = JsonSerializer.Deserialize <User>(ReadSetting("CurrentUser")); Uri colUri = new Uri("http://localhost:4000/CollectiveUsers/RelevantCollectives/" + currentUser.Id); IList <Collective> colList = await CrudHandler.GetGenericArrayAsync <Collective>(colUri, ReadSetting("AuthInfo")).ConfigureAwait(true); foreach (var item in colList) { Collectives.Add(item); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary>Registers the user asynchronous.</summary> /// <param name="firstname">The firstname.</param> /// <param name="lastname">The lastname.</param> /// <param name="username">The username.</param> /// <param name="password">The password.</param> /// <returns></returns> internal async Task <bool> RegisterUserAsync(string firstname, string lastname, string username, string password) { var user = new UserDto() { FirstName = firstname, LastName = lastname, Username = username, Password = password }; var userJson = JsonConvert.SerializeObject(user); var userStringContent = new StringContent(userJson, Encoding.UTF8, "application/json"); if (await CrudHandler.Register(userStringContent).ConfigureAwait(false)) { userStringContent.Dispose(); return(true); } else { userStringContent.Dispose(); } return(false); }
/// <summary>Leaves the collective asynchronous.</summary> internal async Task LeaveCollectiveAsync() { try { var currentUser = System.Text.Json.JsonSerializer.Deserialize <User>(ReadSetting("CurrentUser")); var collective = GetCurrentCollective(); if (currentUser.Id != collective.OwnerId) { Uri colLeave = new Uri("http://localhost:4000/CollectiveUsers/LeaveCollective/" + collective.Id + "+" + currentUser.Id); await CrudHandler.DeleteGenericAsync(ReadSetting("AuthInfo"), colLeave).ConfigureAwait(true); } else { await new MessageDialog("You cannot leave a collective that you own", "Cannot leave").ShowAsync(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary>Joins the collective asynchronous.</summary> /// <param name="collectiveid">The collectiveid.</param> /// <returns></returns> internal async Task <bool> JoinCollectiveAsync(int collectiveid) { var currentUser = System.Text.Json.JsonSerializer.Deserialize <User>(ReadSetting("CurrentUser")); var collectiveuser = new CollectiveUser() { CollectiveId = collectiveid, UserId = currentUser.Id }; var collectiveuserJson = JsonConvert.SerializeObject(collectiveuser); #pragma warning disable CA2000 // Dispose objects before losing scope var userStringContent = new StringContent(collectiveuserJson, Encoding.UTF8, "application/json"); #pragma warning restore CA2000 // Dispose objects before losing scope if (await CrudHandler.JoinCollective(userStringContent, ReadSetting("AuthInfo")).ConfigureAwait(true)) { userStringContent.Dispose(); return(true); } else { return(false); } }
public OpenOrderRepository(CrudHandler <Order> crudHandler) : base(crudHandler) { }
public GroupRepository(CrudHandler <UserGroup> crudHandler) : base(crudHandler) { }