Skip to content
Open
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
28 changes: 28 additions & 0 deletions Contexts/MainAppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class MainAppContext(DbContextOptions<MainAppContext> contextOptions)
public DbSet<TempUser> TempUsers { get; set; }
public DbSet<Bid> Bids { get; set; }
public DbSet<TaskEntity> Tasks { get; set; }
public DbSet<Skill> skills { get; set; }
public DbSet<ProjectLike> ProjectLikes { get; set; }


protected override void OnModelCreating(ModelBuilder builder)
{
Expand Down Expand Up @@ -48,6 +51,9 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.Entity<Project>().ToTable("Projects", tb => tb.HasCheckConstraint("CK_STATUS", "[Status] IN ('Available', 'Closed')"))
.Property(p=>p.Status).HasDefaultValue("Available");

builder.Entity<Skill>()
.ToTable("skills", tb => tb.HasCheckConstraint("CK_NAME", "[Name] IN ('uiux', 'frontend', 'mobile', 'backend', 'fullstack')"));

builder.Entity<Bid>()
.HasOne(b => b.Project)
.WithMany(p => p.Bids)
Expand All @@ -60,9 +66,31 @@ protected override void OnModelCreating(ModelBuilder builder)
.HasForeignKey(b => b.FreelancerId)
.OnDelete(DeleteBehavior.NoAction);

builder.Entity<Skill>()
.HasOne(s => s.freelancer)
.WithMany(f => f.Skills)
.HasForeignKey(b => b.UserId)
.OnDelete(DeleteBehavior.NoAction);

builder.Entity<TaskEntity>().ToTable("Tasks");

builder.Entity<ProjectLike>()
.HasIndex(pl => new { pl.ProjectId, pl.UserId })
.IsUnique();

builder.Entity<ProjectLike>()
.HasOne(p => p.Project)
.WithMany(u => u.projectLikes)
.HasForeignKey(b => b.ProjectId)
.OnDelete(DeleteBehavior.NoAction);

builder.Entity<ProjectLike>()
.HasOne(p => p.user)
.WithMany(u => u.projectLikes)
.HasForeignKey(b => b.UserId)
.OnDelete(DeleteBehavior.NoAction);


base.OnModelCreating(builder);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Controllers/Mobile/v1/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public async Task<IActionResult> CompleteRegistrationAsync([FromBody] RegisterRe
UserName = registerReq.Username,
PhoneNumber = tempUser.PhoneNumber,
PhoneNumberConfirmed = tempUser.PhoneNumberConfirmed,
Skills = registerReq.Skills ?? string.Empty,
//Skills = registerReq.Skills ?? string.Empty,
},
Constants.USER_TYPE_CLIENT => new Client()
{
Expand Down
90 changes: 90 additions & 0 deletions Controllers/Mobile/v1/DashboardController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using AonFreelancing.Contexts;
using AonFreelancing.Interfaces;
using AonFreelancing.Models;
using AonFreelancing.Models.DTOs;
using AonFreelancing.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Runtime.Serialization.Formatters;

namespace AonFreelancing.Controllers.Mobile.v1
{
[ApiController]
[Route("api/mobile/v1")]
public class DashboardController : BaseController
{
private readonly MainAppContext _mainAppContext;
private readonly UserManager<User> _userManager;
private readonly IStatisticsService _statisticsService;

public DashboardController(MainAppContext mainAppContext, IStatisticsService statisticsService, UserManager<User> userManager)
{
_mainAppContext = mainAppContext;
_statisticsService = statisticsService;
_userManager = userManager;
}

[HttpGet("GetMyStatistics")]
public async Task<IActionResult> GetMyStatistics()
{
var user = await _userManager.GetUserAsync(HttpContext.User);
if (user == null)
return NotFound(CreateErrorResponse(StatusCodes.Status404NotFound.ToString(),
"Unable to load user."));

var userId = user.Id;

var projects = await _mainAppContext.Projects
.Where(p => p.FreelancerId == userId || p.ClientId == userId)
.ToListAsync();

var tasks = await _mainAppContext.Tasks
.Where(t => !t.IsDeleted && projects.Select(p => p.Id).Contains(t.ProjectId))
.ToListAsync();

// Project statistics
int totalProjects = projects.Count();
int availableProjects = projects.Count(p => p.Status == Constants.PROJECT_STATUS_AVAILABLE);
int closedProjects = projects.Count(p => p.Status == Constants.PROJECT_STATUS_CLOSED);

// Skill issues here (Diyar)
//int totalTasks = tasks.Count;
//int toDoTasks = tasks.Count(t => t.Status.Trim().Equals(Constants.TASKS_STATUS_TO_DO, StringComparison.OrdinalIgnoreCase));
//int inProgressTasks = tasks.Count(t => t.Status.Trim().Equals(Constants.TASKS_STATUS_IN_PROGRESS, StringComparison.OrdinalIgnoreCase));
//int inReviewTasks = tasks.Count(t => t.Status.Trim().Equals(Constants.TASKS_STATUS_IN_REVIEW, StringComparison.OrdinalIgnoreCase));
//int doneTasks = tasks.Count(t => t.Status.Trim().Equals(Constants.TASKS_STATUS_DONE, StringComparison.OrdinalIgnoreCase));

// Task statistics
int totalTasks = tasks.Count;
int toDoTasks = tasks.Count(t => t.Status == Constants.TASKS_STATUS_TO_DO);
int inProgressTasks = tasks.Count(t => t.Status == Constants.TASKS_STATUS_IN_PROGRESS);
int inReviewTasks = tasks.Count(t => t.Status == Constants.TASKS_STATUS_IN_REVIEW);
int doneTasks = tasks.Count(t => t.Status == Constants.TASKS_STATUS_DONE);


// Prepare response
var response = new StatisticsResponseDTO
{
Projects = new ProjectStatistics
{
Total = totalProjects,
Available = availableProjects,
Closed = closedProjects
},
Tasks = new TaskStatistics
{
Total = totalTasks,
ToDo = _statisticsService.CalculatePercentage(totalTasks, toDoTasks),
InProgress = _statisticsService.CalculatePercentage(totalTasks, inProgressTasks),
InReview = _statisticsService.CalculatePercentage(totalTasks, inReviewTasks),
Done = _statisticsService.CalculatePercentage(totalTasks, doneTasks)
}
};

return Ok(CreateSuccessResponse(response));
}

}
}
Loading