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

Zh/modules search #61

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
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
130 changes: 126 additions & 4 deletions CSLabs.Api/Controllers/ModuleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;

namespace CSLabs.Api.Controllers
{
Expand Down Expand Up @@ -104,17 +105,138 @@ public async Task<IActionResult> GetForModuleEditor(int id)

// GET api/module/modules-editor
[HttpGet("modules-editor")]
public async Task<IActionResult> GetUsersModules()
public async Task<IActionResult> GetEditorsModules()
{
if (!GetUser().CanEditModules()) {
return Forbid("You are not allowed to edit modules");
}
var query = this.DatabaseContext.Modules.AsQueryable();
if (!GetUser().IsAdmin())
query = query.Where(m => m.OwnerId == GetUser().Id).IncludeTags();
var query = DatabaseContext.Modules
.Where(m => m.OwnerId == GetUser().Id)
.IncludeTags();
return Ok(await query.ToListAsync());
}

// GET api/module/modules-editor/admin
[HttpGet("modules-editor/admin")]
public async Task<IActionResult> GetAdminModules()
{
if (!GetUser().IsAdmin()) {
return Forbid("You are not allowed to access these modules");
}
return Ok(
await DatabaseContext.Modules.IncludeTags().ToListAsync()
);
}

// GET api/module/modules-editor/search/{searchTerm}
[HttpGet("modules-editor/search/{searchTerm}")]
public async Task<IActionResult> SearchEditorsModules(string searchTerm)
{
if (!GetUser().CanEditModules())
return Forbid("You are not allowed to edit modules");

if (string.IsNullOrEmpty(searchTerm))
return Ok(await DatabaseContext.Modules.ToListAsync());

var searchedModules = DatabaseContext.Modules.AsQueryable();
if (!GetUser().IsAdmin())
searchedModules = searchedModules.Where(m => m.OwnerId == GetUser().Id);

searchedModules = searchedModules
.Where(m => EF.Functions.Match(m.Name, searchTerm, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Match(m.Description, searchTerm, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Like(m.Name, $"%{searchTerm}%") ||
EF.Functions.Like(m.Description, $"%{searchTerm}%") ||
m.ModuleTags.Any(mt => searchTerm.Equals(mt.Tag.Name.ToLower())));

return Ok(await searchedModules.ToListAsync());
}

// GET api/module/modules-editor/search?params={...}
[HttpGet("modules-editor/search")]
public async Task<IActionResult> SearchOptionsEditorsModules(string title = null, string description = null, int difficulty = 0, string tags = null)
{
if (!GetUser().CanEditModules())
return Forbid("You are not allowed to edit modules");

var searchedModules = DatabaseContext.Modules.AsQueryable();
if (GetUser().IsAdmin())
searchedModules = searchedModules.Where(m => m.OwnerId == GetUser().Id);

if (!string.IsNullOrEmpty(title))
searchedModules = searchedModules
.Where(m => EF.Functions.Match(m.Name, title, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Like(m.Name, $"%{title}%"));

if (!string.IsNullOrEmpty(description))
searchedModules = searchedModules
.Where(m => EF.Functions.Match(m.Description, description, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Like(m.Description, $"%{description}%"));

if (difficulty != 0)
searchedModules = searchedModules
.Where(m => m.Difficulty == difficulty);

if (!string.IsNullOrEmpty(tags))
{
var tagNamesList = tags.Split(",").Select(s => s.Trim()).ToList();
searchedModules = searchedModules
.Where(m => m.ModuleTags.Any(mt => tagNamesList.Contains(mt.Tag.Name)))
.IncludeTags();
}
return Ok(await searchedModules.ToListAsync());
}

// GET api/module/search/{searchTerm}
[HttpGet("search/{searchTerm}")]
[AllowAnonymous]
public async Task<IActionResult> Search(string searchTerm)
{
if (string.IsNullOrEmpty(searchTerm))
return Ok(await DatabaseContext.Modules.Where(m => m.Published).ToListAsync());

var searchedModules = DatabaseContext.Modules
.Where(m => m.Published)
.Where(m => EF.Functions.Match(m.Name, searchTerm, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Match(m.Description, searchTerm, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Like(m.Name, $"%{searchTerm}%") ||
EF.Functions.Like(m.Description, $"%{searchTerm}%") ||
m.ModuleTags.Any(mt => searchTerm.Equals(mt.Tag.Name.ToLower())));

return Ok(await searchedModules.ToListAsync());
}

// GET api/module/search?params={...}
[HttpGet("search")]
[AllowAnonymous]
public async Task<IActionResult> SearchOptions(string title = null, string description = null, int difficulty = 0, string tags = null)
{
var searchedModules = DatabaseContext.Modules.Where(m => m.Published);

if (!string.IsNullOrEmpty(title))
searchedModules = searchedModules
.Where(m => EF.Functions.Match(m.Name, title, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Like(m.Name, $"%{title}%"));

if (!string.IsNullOrEmpty(description))
searchedModules = searchedModules
.Where(m => EF.Functions.Match(m.Description, description, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Like(m.Description, $"%{description}%"));

if (difficulty != 0)
searchedModules = searchedModules
.Where(m => m.Difficulty == difficulty);

if (!string.IsNullOrEmpty(tags))
{
var tagNamesList = tags.Split(",").Select(s => s.Trim()).ToList();
searchedModules = searchedModules
.Where(m => m.ModuleTags.Any(mt => tagNamesList.Contains(mt.Tag.Name)))
.IncludeTags();
}
return Ok(await searchedModules.ToListAsync());
}

// POST api/values
[HttpPost]
public async Task<IActionResult> Upsert([FromBody] Module module)
Expand Down
50 changes: 50 additions & 0 deletions CSLabs.Api/Controllers/UserModuleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,55 @@ public async Task<IActionResult> Get(int id)
return NotFound();
return Ok(module);
}

// GET api/user-module/search/{searchTerm}
[HttpGet("search/{searchTerm}")]
public async Task<IActionResult> Search(string searchTerm)
{
if (string.IsNullOrEmpty(searchTerm))
return Ok(await DatabaseContext.UserModules.ToListAsync());

var searchedModules = DatabaseContext.UserModules
.WhereIncludesUser(GetUser())
.Include(um => um.Module)
.Where(um => EF.Functions.Match(um.Module.Name, searchTerm, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Match(um.Module.Description, searchTerm, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Like(um.Module.Name, $"%{searchTerm}%") ||
EF.Functions.Like(um.Module.Description, $"%{searchTerm}%") ||
um.Module.ModuleTags.Any(mt => searchTerm.Equals(mt.Tag.Name.ToLower())));

return Ok(await searchedModules.ToListAsync());
}

// GET api/user-module/search?params={...}
[HttpGet("search")]
public async Task<IActionResult> SearchOptions(string title = null, string description = null, int difficulty = 0, string tags = null)
{
var searchedModules = DatabaseContext.UserModules
.Include(um => um.Module)
.WhereIncludesUser(GetUser());

if (!string.IsNullOrEmpty(title))
searchedModules = searchedModules
.Where(um => EF.Functions.Match(um.Module.Name, title, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Like(um.Module.Name, $"%{title}%"));

if (!string.IsNullOrEmpty(description))
searchedModules = searchedModules
.Where(um => EF.Functions.Match(um.Module.Description, description, MySqlMatchSearchMode.NaturalLanguage) ||
EF.Functions.Like(um.Module.Description, $"%{description}%"));

if (difficulty != 0)
searchedModules = searchedModules
.Where(um => um.Module.Difficulty == difficulty);

if (!string.IsNullOrEmpty(tags))
{
var tagNamesList = tags.Split(",").Select(s => s.Trim()).ToList();
searchedModules = searchedModules
.Where(um => um.Module.ModuleTags.Any(mt => tagNamesList.Contains(mt.Tag.Name)));
}
return Ok(await searchedModules.ToListAsync());
}
}
}
Loading