Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

Commit

Permalink
#28 WebAppAPIAdding
Browse files Browse the repository at this point in the history
  • Loading branch information
Nypifok committed Dec 11, 2020
1 parent b2357d2 commit ea7bfb5
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 3 deletions.
11 changes: 11 additions & 0 deletions ImageBase.WebApp/Controllers/ImagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ private async Task<string> CurrentUser()
return null;
}
}
[AllowAnonymous]
[HttpGet("[action]")]
public async Task<ActionResult<ImageDto>> Search([FromBody] FullTextSeacrhDto fullTextSeacrhDto)
{
ServiceResponse<PaginationListDto<Image>> serviceSearch = await _imagesService.FullTextSearchByImagesAsync(fullTextSeacrhDto);
if (serviceSearch.Success == false)
{
return Forbid(serviceSearch.Message);
}
return Ok(serviceSearch);
}

}
}
26 changes: 26 additions & 0 deletions ImageBase.WebApp/Data/Dtos/FullTextSeacrhDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ImageBase.WebApp.Data.Dtos
{
public class FullTextSeacrhDto
{
public string SearchQuery { get; set; }
public int Limit { get; set; }
public int Offset { get; set; }
public bool IncludeTitle { get; set; }
public bool IncludeDescription { get; set; }
public bool IncludeKeyWords { get; set; }

public string ConvertToPostgreFTSWeights()
{
string result="";
if (IncludeTitle) result += "A";
if (IncludeKeyWords) result += "B";
if (IncludeDescription) result += "C";
return result;
}
}
}
25 changes: 25 additions & 0 deletions ImageBase.WebApp/Migrations/20201204105242_FTSIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ ORDER BY weight ASC
WHERE images.id = ids.image_id
$$ LANGUAGE SQL IMMUTABLE; ");
migrationBuilder.Sql(@"CREATE OR REPLACE FUNCTION remove_ft_index() RETURNS void AS $$
BEGIN
DROP INDEX image_vector_idx;
END;
$$ LANGUAGE plpgsql;");
migrationBuilder.Sql(@"CREATE OR REPLACE FUNCTION disable_ft_trigger() RETURNS void AS $$
BEGIN
ALTER TABLE images DISABLE TRIGGER image_search_vector_update;
END;
$$ LANGUAGE plpgsql;");
migrationBuilder.Sql(@"CREATE OR REPLACE FUNCTION enable_ft_trigger() RETURNS void AS $$
BEGIN
ALTER TABLE images ENABLE TRIGGER image_search_vector_update;
END;
$$ LANGUAGE plpgsql;");
migrationBuilder.Sql(@"CREATE OR REPLACE FUNCTION create_ft_index() RETURNS void AS $$
BEGIN
CREATE INDEX IF NOT EXISTS image_vector_idx ON images_ft_search
USING RUM (image_vector);
END;
$$ LANGUAGE plpgsql;");
}

protected override void Down(MigrationBuilder migrationBuilder)
Expand All @@ -122,6 +143,10 @@ protected override void Down(MigrationBuilder migrationBuilder)
migrationBuilder.Sql(@"DROP FUNCTION images_fts;");
migrationBuilder.Sql(@"DROP FUNCTION setweight;");
migrationBuilder.Sql(@"DROP FUNCTION image_tsvector_trigger;");
migrationBuilder.Sql(@"DROP FUNCTION create_ft_index;");
migrationBuilder.Sql(@"DROP FUNCTION remove_ft_index;");
migrationBuilder.Sql(@"DROP FUNCTION disable_ft_trigger;");
migrationBuilder.Sql(@"DROP FUNCTION enable_ft_trigger;");
}
}
}
2 changes: 1 addition & 1 deletion ImageBase.WebApp/Repositories/CatalogRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void DeleteImageFromCatalog(ImageCatalog imageCatalog)
_context.ImageCatalogs.Remove(imageCatalog);
}

public async Task<PaginationListDto<Image>> GetImagesByCatalogAsync(long id, int offset, int limit)
public async Task<PaginationListDto <Image>> GetImagesByCatalogAsync(long id, int offset, int limit)
{
IQueryable<Image> query = _context.ImageCatalogs.Where(ic => ic.CatalogId == id)
.Include(ic => ic.Image)
Expand Down
4 changes: 3 additions & 1 deletion ImageBase.WebApp/Repositories/IImageRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ImageBase.WebApp.Data.Models;
using ImageBase.WebApp.Data.Dtos;
using ImageBase.WebApp.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -10,5 +11,6 @@ public interface IImageRepository: IRepository<Image, long>
{
Task<bool> IsImageTitleAlreadyExists(int id, string title);
Task<bool> HasCatalogWithUserIdAsync(int? id, string userId);
Task<PaginationListDto<Image>> GetImagesBySearchQueryAsync(FullTextSeacrhDto query);
}
}
17 changes: 16 additions & 1 deletion ImageBase.WebApp/Repositories/ImageRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ImageBase.WebApp.Data.Models;
using ImageBase.WebApp.Data.Dtos;
using ImageBase.WebApp.Data.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -62,5 +63,19 @@ public async Task<bool> HasCatalogWithUserIdAsync(int? id, string userId)
{
return await _context.Catalogs.Where(c => c.Id == id && c.UserId == userId).AnyAsync();
}

public async Task<PaginationListDto<Image>> GetImagesBySearchQueryAsync(FullTextSeacrhDto query)
{
var weights=query.ConvertToPostgreFTSWeights();
var images = await _context.Images
.FromSqlInterpolated($@"SELECT * FROM images_fts({query.SearchQuery},{weights},{query.Limit},{query.Offset});").ToListAsync();
return new PaginationListDto<Image>
{
Items = images,
TotalItemsCount = images.Count(),
Limit=query.Limit,
Offset=query.Offset
};
}
}
}
8 changes: 8 additions & 0 deletions ImageBase.WebApp/Services/Implementations/ImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,13 @@ public async Task<ServiceResponse<ImageDto>> CreateImageAsync(AddImageDto addIma

return serviceResponse;
}

public async Task<ServiceResponse<PaginationListDto<Image>>> FullTextSearchByImagesAsync(FullTextSeacrhDto queryDto)
{
var serviceResponse = new ServiceResponse<PaginationListDto<Image>>();
serviceResponse.Data = await _repository.GetImagesBySearchQueryAsync(queryDto);
serviceResponse.Success = true;
return serviceResponse;
}
}
}
2 changes: 2 additions & 0 deletions ImageBase.WebApp/Services/Interfaces/IImageService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ImageBase.WebApp.Data.Dtos;
using ImageBase.WebApp.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,5 +10,6 @@ namespace ImageBase.WebApp.Services.Interfaces
public interface IImageService
{
Task<ServiceResponse<ImageDto>> CreateImageAsync(AddImageDto addImageDto, string userId = null);
Task<ServiceResponse<PaginationListDto<Image>>> FullTextSearchByImagesAsync(FullTextSeacrhDto queryDto);
}
}

0 comments on commit ea7bfb5

Please sign in to comment.