/// <summary> /// Edits a given post /// </summary> /// <param name="actualRequest">the client request to be handled</param> /// <returns>the response to the given request</returns> private async Task <ActualRequest> UpdatePostAsync(ActualRequest actualRequest) { Request request = actualRequest.Request; PostShortVersion updatedPost = JsonSerializer.Deserialize <PostShortVersion>(request.Argument.ToString()); bool result = await postRepo.EditPostAsync(updatedPost); Request responseRequest = new Request { ActionType = ActionType.POST_EDIT.ToString(), Argument = JsonSerializer.Serialize(result) }; if (!result) { return new ActualRequest { Request = responseRequest, Images = null } } ; if (actualRequest.Images != null && actualRequest.Images.Any()) { ImagesUtil.WriteImageToPath(actualRequest.Images[0], $"{FILE_PATH}/Posts", $"/{updatedPost.Id}.jpg"); } return(new ActualRequest { Request = responseRequest, Images = null }); }
/// <summary> /// Persists a given post to the database /// </summary> /// <param name="actualRequest">the client request to be handled</param> /// <returns>the response to the given request</returns> private async Task <ActualRequest> AddPostAsync(ActualRequest actualRequest) { Request request = actualRequest.Request; PostShortVersion post = JsonSerializer.Deserialize <PostShortVersion>(request.Argument.ToString()); post.HasImage = actualRequest.Images != null; Console.WriteLine("Post Sockets adding post " + post.Title); int result = await postRepo.AddPostAsync(post); Request responseRequest = new Request { ActionType = ActionType.POST_CREATE.ToString(), Argument = JsonSerializer.Serialize(result) }; if (result > 0) { if (post.HasImage) { try { ImagesUtil.WriteImageToPath(actualRequest.Images[0], $"{FILE_PATH}/Posts", $"/{result}.jpg"); } catch (Exception e) { Console.WriteLine("Could not add image to created post " + result); } } } return(new ActualRequest { Request = responseRequest, Images = null }); }
/// <summary> /// Retrieves a post with its owner, by id and with the sender status regarding it /// </summary> /// <param name="actualRequest">the client request to be handled</param> /// <returns>the response to the given request</returns> private async Task <ActualRequest> GetPostByIdAsync(ActualRequest actualRequest) { Request request = actualRequest.Request; List <int> ints = JsonSerializer.Deserialize <List <int> >(request.Argument.ToString()); PostShortVersion post = await postRepo.GetPostByIdAsync(ints[0], ints[1]); Request responseRequest = new Request { ActionType = ActionType.POST_GET_BY_ID.ToString(), Argument = JsonSerializer.Serialize(post) }; List <byte[]> images = new List <byte[]>(); if (post != null) { var readOwnerAvatar = File.ReadAllBytes($"{FILE_PATH}/Users/{post.Owner.UserId}/avatar.jpg"); images.Add(ImagesUtil.ResizeImage(readOwnerAvatar, 20, 20)); if (post.HasImage) { var readAvatarFile = File.ReadAllBytes($"{FILE_PATH}/Posts/{post.Id}.jpg"); images.Add(readAvatarFile); } } return(new ActualRequest { Request = responseRequest, Images = images }); }
public async Task <int> AddPostAsync(PostShortVersion postShortVersion) { using (ShapeAppDbContext ctx = new ShapeAppDbContext()) { User owner = await ctx.Users.FirstAsync(u => u.Id == postShortVersion.Owner.UserId); Post post = new Post { Title = postShortVersion.Title, Content = postShortVersion.Content, Owner = owner, TimeStamp = DateTime.Now, HasImage = postShortVersion.HasImage }; Console.WriteLine("adding post to db"); await ctx.Posts.AddAsync(post); await ctx.SaveChangesAsync(); Console.WriteLine("post saved"); var newPost = await ctx.Posts.ToListAsync(); return(newPost.Last().Id); } }
public async Task <bool> EditPostAsync(PostShortVersion post) { using (ShapeAppDbContext ctx = new ShapeAppDbContext()) { Post postDb = await ctx.Posts.FirstAsync(p => p.Id == post.Id); if (post.Title != null) { postDb.Title = post.Title; } if (post.Content != null) { postDb.Content = post.Content; } try { ctx.Posts.Update(postDb); await ctx.SaveChangesAsync(); } catch (Exception e) { return(false); } return(true); } }
public async Task EditPost(PostShortVersion postShortVersion) { await client.PutAsync($"{uri}/{postShortVersion.Id}", PrepareObjectForRequest(postShortVersion)); }
public async Task <int> AddNewPost(PostShortVersion post) { HttpResponseMessage response = await client.PostAsync(uri, PrepareObjectForRequest(post)); return(int.Parse(await response.Content.ReadAsStringAsync())); }