Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: post in perfil and index #23

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions YouCode.BE/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ public class User
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
public string? Name { get; set; }
[Required]
public string Username { get; set; } = string.Empty;

// [Required]
public DateTime CreatedAt { get; set; } = DateTime.Now;


}
}
2 changes: 1 addition & 1 deletion YouCode.GUI/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task<IActionResult> Login(string code)

userDB = new BE.User
{
Name = userGithub.Name,
Name = userGithub.Name == null ? userGithub.Login : userGithub.Name,
Username = userGithub.Login,
CreatedAt = DateTime.UtcNow
};
Expand Down
8 changes: 6 additions & 2 deletions YouCode.GUI/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
using System.Security.Claims;
using YouCode.BL;
using YouCode.GUI.Models;
using YouCode.GUI.Services;
using YouCode.GUI.Services.Auth;
namespace YouCode.GUI.Controllers
{
public class HomeController : Controller
{
private readonly PostService postService = new PostService();

private readonly ILogger<HomeController> _logger;

private readonly IConfiguration _configuration;
Expand All @@ -23,9 +26,10 @@ public HomeController(ILogger<HomeController> logger, IConfiguration configurati

}
[AllowAnonymous]
public IActionResult Index()
public async Task<IActionResult> Index()
{
return View();
var posts = await postService.GetAllAsync(null);
return View(posts);
}

public IActionResult Privacy()
Expand Down
11 changes: 9 additions & 2 deletions YouCode.GUI/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Authorization;
using YouCode.GUI.Services.Auth;
using YouCode.GUI.Services;
using YouCode.GUI.Models.DTOs;

namespace YouCode.GUI.Controllers;
public class UserController : Controller
Expand All @@ -23,9 +24,15 @@ public async Task<IActionResult> Profile(string username)
if(user != null)
{
var profile = await profileBL.GetByIdAsync(new Profile { Id = user.Id });
var posts = await postService.GetAllAsync(user.Username);
Console.WriteLine(user.Username);

if(profile != null)
{
return View(profile);
return View(new ProfileReturnDto(){
Profile = profile,
Posts = posts
});
}
else
{
Expand All @@ -43,7 +50,7 @@ public async Task<IActionResult> Profile(string username)
[JwtAuthentication]
public async Task<IActionResult> Feed()
{
var posts = await postService.GetAllAsync();
var posts = await postService.GetAllAsync(null);
return View(posts);
}
public async Task<IActionResult> Edit(int id)
Expand Down
13 changes: 13 additions & 0 deletions YouCode.GUI/Models/DTOs/PostDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using YouCode.BE;

namespace YouCode.GUI.Models.DTOs
{
public class PostDto : Post
{
public Profile? UserProfile { get; set; }
public List<Image>? Images { get; set; }
public int FavoriteCount { get; set; }
public List<Reaction>? Reactions { get; set; }

}
}
10 changes: 10 additions & 0 deletions YouCode.GUI/Models/DTOs/ProfileReturnDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using YouCode.BE;

namespace YouCode.GUI.Models.DTOs
{
public class ProfileReturnDto
{
public Profile Profile { get; set;} = null!;
public List<PostDto>? Posts { get; set; }
}
}
23 changes: 12 additions & 11 deletions YouCode.GUI/Services/PostService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using YouCode.BE;
using YouCode.BL;
using YouCode.GUI.Models.DTOs;

namespace YouCode.GUI.Services
{
Expand All @@ -13,11 +14,20 @@ public class PostService
private readonly ReactionBL reactionBL = new ReactionBL();


public async Task<List<PostDto>> GetAllAsync()
public async Task<List<PostDto>> GetAllAsync(string? username)
{
var posts = await postBL.GetAllAsync();

var postFiltered = posts.FindAll(p => {
if (username == null)
{
return true;
}
return p.User.Username == username;
});
var postDtos = new List<PostDto>();
foreach (var post in posts)

foreach (var post in postFiltered)
{
var profile = await profileBL.GetByIdAsync(new Profile { Id = post.User.Id });
var Allimages = await imageBL.GetAllAsync();
Expand Down Expand Up @@ -45,13 +55,4 @@ public async Task<List<PostDto>> GetAllAsync()
return postDtos;
}
}

public class PostDto : Post
{
public Profile? UserProfile { get; set; }
public List<Image>? Images { get; set; }
public int FavoriteCount { get; set; }
public List<Reaction>? Reactions { get; set; }

}
}
Loading
Loading