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
47 changes: 11 additions & 36 deletions src/Analysim.Web/Controllers/ProjectController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Internal;
using Internal;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -12,6 +12,7 @@
using System.Threading.Tasks;
using Infrastructure.Data;
using Core.Interfaces;
using Web.Repositories;
using Web.ViewModels.Project;
using Core.Entities;
using System.Net.Http;
Expand Down Expand Up @@ -39,11 +40,13 @@ public class ProjectController : ControllerBase

private readonly ApplicationDbContext _dbContext;
private readonly IConfiguration _configuration;
private readonly IProjectRepository _projectRepository;

public ProjectController(ApplicationDbContext dbContext, IConfiguration configuration)
public ProjectController(ApplicationDbContext dbContext, IConfiguration configuration, IProjectRepository projectRepository)
{
_dbContext = dbContext;
_configuration = configuration;
_projectRepository = projectRepository;
}

#region GET REQUEST
Expand All @@ -58,12 +61,7 @@ public IActionResult GetProjectByID([FromRoute] int projectID)
{
// Find Project
// Include To Many List
var project = _dbContext.Projects
.Include(p => p.BlobFiles)
.Include(p => p.ProjectUsers)
.Include(p => p.Notebooks)
.Include(p => p.ProjectTags).ThenInclude(pt => pt.Tag)
.SingleOrDefault(p => p.ProjectID == projectID);
var project = _projectRepository.GetProjectById(projectID);
if (project == null) return NotFound(new { message = "Project Not Found" });

// Return Ok Request
Expand All @@ -84,12 +82,7 @@ public IActionResult GetProjectByID([FromRoute] int projectID)
public IActionResult GetProjectByRoute([FromRoute] string owner, [FromRoute] string projectname)
{
// Find Project
var project = _dbContext.Projects
.Include(p => p.BlobFiles)
.Include(p => p.Notebooks)
.Include(p => p.ProjectUsers).ThenInclude(pu => pu.User)
.Include(p => p.ProjectTags).ThenInclude(pt => pt.Tag)
.SingleOrDefault(p => p.Route.ToLower() == owner.ToLower() + "/" + projectname.ToLower());
var project = _projectRepository.GetProjectByRoute(owner, projectname);
if (project == null) return NotFound(new { message = "Project Not Found" });

// Return Ok Request
Expand All @@ -109,12 +102,7 @@ public IActionResult GetProjectByRoute([FromRoute] string owner, [FromRoute] str
public IActionResult GetProjectRange([FromQuery(Name = "id")] List<int> idList)
{
// Find Project
var projects = _dbContext.Projects
.Include(p => p.BlobFiles)
.Include(p => p.ProjectUsers)
.Include(p => p.ProjectTags).ThenInclude(pt => pt.Tag)
.Where(p => idList.Contains(p.ProjectID))
.ToList();
var projects = _projectRepository.GetProjectRange(idList);

// Return Ok Request
return Ok(new
Expand All @@ -134,11 +122,7 @@ public IActionResult GetProjectList()
{

// Get All Project And Include To Many List
var projects = _dbContext.Projects
.Include(p => p.BlobFiles)
.Include(p => p.ProjectUsers)
.Include(p => p.ProjectTags).ThenInclude(pt => pt.Tag)
.ToList();
var projects = _projectRepository.GetProjectList();

// Return Ok Request
return Ok(new
Expand All @@ -156,17 +140,8 @@ public IActionResult GetProjectList()
[HttpGet("[action]")]
public IActionResult Search([FromQuery(Name = "term")] List<string> searchTerms)
{
var matchedTag = _dbContext.Tag
.ToList()
.Where(t => searchTerms.Any(st => t.Name.ToLower().Contains(st.ToLower())));

var matchedProject = _dbContext.Projects
.Include(p => p.BlobFiles)
.Include(p => p.ProjectUsers)
.Include(p => p.ProjectTags).ThenInclude(pt => pt.Tag)
.ToList()
.Where(p => matchedTag.Any(mt => p.ProjectTags.Any(pt => pt.Tag.Name.ToLower() == mt.Name.ToLower())));
if (matchedProject.Count() == 0) return NoContent();
var matchedProject = _projectRepository.Search(searchTerms);
if (matchedProject.Count == 0) return NoContent();

return Ok(new
{
Expand Down
4 changes: 3 additions & 1 deletion src/Analysim.Web/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Infrastructure.Data;
using Infrastructure.Data;
using Core.Helper;
using Core.Entities;
using Core.Services;
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Web.Repositories;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -68,6 +69,7 @@ public static void ConfigureDatabase(this IServiceCollection services, IConfigur
services.AddEntityFrameworkNpgsql()
.AddDbContext<ApplicationDbContext>(opt => opt.UseNpgsql(configuration.GetConnectionString("DBConnectionString"),
x => x.MigrationsAssembly("Analysim.Infrastructure")));
services.AddScoped<IProjectRepository, ProjectRepository>();
}

public static void ConfigureJWT(this IServiceCollection services, IConfiguration configuration)
Expand Down
14 changes: 14 additions & 0 deletions src/Analysim.Web/Repositories/IProjectRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using Core.Entities;

namespace Web.Repositories
{
public interface IProjectRepository
{
Project GetProjectById(int projectID);
Project GetProjectByRoute(string owner, string projectname);
List<Project> GetProjectRange(List<int> idList);
List<Project> GetProjectList();
List<Project> Search(List<string> searchTerms);
}
}
74 changes: 74 additions & 0 deletions src/Analysim.Web/Repositories/ProjectRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Generic;
using System.Linq;
using Core.Entities;
using Infrastructure.Data;
using Microsoft.EntityFrameworkCore;

namespace Web.Repositories
{
public class ProjectRepository : IProjectRepository
{
private readonly ApplicationDbContext _context;

public ProjectRepository(ApplicationDbContext context)
{
_context = context;
}

public Project GetProjectById(int projectID)
{
return _context.Projects
.Include(p => p.BlobFiles)
.Include(p => p.ProjectUsers)
.Include(p => p.Notebooks)
.Include(p => p.ProjectTags).ThenInclude(pt => pt.Tag)
.SingleOrDefault(p => p.ProjectID == projectID);
}

public Project GetProjectByRoute(string owner, string projectname)
{
return _context.Projects
.Include(p => p.BlobFiles)
.Include(p => p.Notebooks)
.Include(p => p.ProjectUsers).ThenInclude(pu => pu.User)
.Include(p => p.ProjectTags).ThenInclude(pt => pt.Tag)
.SingleOrDefault(p => p.Route.ToLower() == owner.ToLower() + "/" + projectname.ToLower());
}

public List<Project> GetProjectRange(List<int> idList)
{
return _context.Projects
.Include(p => p.BlobFiles)
.Include(p => p.ProjectUsers)
.Include(p => p.ProjectTags).ThenInclude(pt => pt.Tag)
.Where(p => idList.Contains(p.ProjectID))
.ToList();
}

public List<Project> GetProjectList()
{
return _context.Projects
.Include(p => p.BlobFiles)
.Include(p => p.ProjectUsers)
.Include(p => p.ProjectTags).ThenInclude(pt => pt.Tag)
.ToList();
}

public List<Project> Search(List<string> searchTerms)
{
var matchedTag = _context.Tag
.ToList()
.Where(t => searchTerms.Any(st => t.Name.ToLower().Contains(st.ToLower())));

var matchedProject = _context.Projects
.Include(p => p.BlobFiles)
.Include(p => p.ProjectUsers)
.Include(p => p.ProjectTags).ThenInclude(pt => pt.Tag)
.ToList()
.Where(p => matchedTag.Any(mt => p.ProjectTags.Any(pt => pt.Tag.Name.ToLower() == mt.Name.ToLower())))
.ToList();

return matchedProject;
}
}
}