Skip to content

Instantly share code, notes, and snippets.

@caominhdev
Forked from nichealpham/Business.cs
Created September 17, 2022 07:06
Show Gist options
  • Save caominhdev/c999c7ec7f65cf78f8f7d9c0a934c436 to your computer and use it in GitHub Desktop.
Save caominhdev/c999c7ec7f65cf78f8f7d9c0a934c436 to your computer and use it in GitHub Desktop.
public static class EpisodeHelper
{
// PERFORMANCE AUDIT !!!
public static IQueryable<Episode> GetEpisodeQueryable(this ERPContext _Context)
{
return _Context.Episodes
.AsNoTracking()
.Include(a => a.Clinic)
.Include(a => a.UserAdmitted)
.Include(a => a.Records)
.ThenInclude(r => r.UserAppoint)
.Include(a => a.Records)
.ThenInclude(r => r.Service)
.Include(a => a.Invoices);
}
// PERFORMANCE AUDIT !!!
public static SearchResult<EpisodeModel> GetEpisodeResults(this IQueryable<Episode> query, string keySearch = "", bool deleted = false, int page = 1, int limit = 10)
{
if (!string.IsNullOrEmpty(keySearch))
{
query = query.Where(a =>
(a.PublicTitle != null && ApplicationDBContext.Standardizing(a.PublicTitle).Contains(keySearch)) ||
(a.UserAdmitted != null && ApplicationDBContext.Standardizing(a.UserAdmitted.FullName).Contains(keySearch)) ||
(a.UserAdmitted != null && !string.IsNullOrEmpty(a.UserAdmitted.Phone) && a.UserAdmitted.Phone.Contains(phoneSearch)) ||
(a.Records != null && a.Records.Any(r =>
(r.UserAppoint != null && ApplicationDBContext.Standardizing(r.UserAppoint.FullName).Contains(keySearch)) ||
(r.Service != null && ApplicationDBContext.Standardizing(r.Service.ServiceName).Contains(keySearch))
)));
}
if (deleted)
{
query = query.Where(a => a.StatusID == (byte)EpisodeStatusType.Deleted);
}
else
{
query = query.Where(a => a.StatusID != (byte)EpisodeStatusType.Deleted);
}
var totals = query.Count();
var items = query
.OrderByDescending(p => p.DateCreated)
.Skip((page - 1) * limit)
.Take(limit)
.Select(EpisodeModel.Convert)
.ToList();
return new SearchResult<EpisodeModel>()
{
Totals = totals,
Items = items
};
}
}
public class ApplicationDBContext : DbContext
{
public ApplicationDBContext() : base()
{
}
public ApplicationDBContext(DbContextOptions options) : base(options)
{
Database.EnsureCreated();
}
[DbFunction]
public static string Standardizing(string input) => throw new NotImplementedException("This method must existed in DB Functions!");
}
USE [EDU]
GO
/****** Object: UserDefinedFunction [dbo].[Standardizing] Script Date: 9/15/2022 2:00:21 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Standardizing] (
@input nvarchar(max)
)
RETURNS varchar(max)
AS
BEGIN
DECLARE @count int;
DECLARE @withaccents nvarchar(100);
DECLARE @withoutaccents nvarchar(100);
DECLARE @result nvarchar(max);
SELECT @result = @input;
-- ACCENTS
SET @withaccents = N'ăâđêôơưàảãạáằẳẵặắầẩẫậấèẻẽẹéềểễệếìỉĩịíòỏõọóồổỗộốờởỡợớùủũụúừửữựứỳỷỹỵýĂÂĐÊÔƠƯÀẢÃẠÁẰẲẴẶẮẦẨẪẬẤÈẺẼẸÉỀỂỄỆẾÌỈĨỊÍÒỎÕỌÓỒỔỖỘỐỜỞỠỢỚÙỦŨỤÚỪỬỮỰỨỲỶỸỴÝ';
SET @withoutaccents = N'aadeoouaaaaaaaaaaaaaaaeeeeeeeeeeiiiiiooooooooooooooouuuuuuuuuuyyyyyAADEOOUAAAAAAAAAAAAAAAEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOUUUUUUUUUUYYYYY';
SET @count = LEN(@withaccents);
WHILE (@count > 0)
BEGIN
SET @result = REPLACE(@result, SUBSTRING(@withaccents, @count, 1), SUBSTRING(@withoutaccents, @count, 1));
SET @count = @count - 1;
END
RETURN LOWER(@result);
END
public static class StringExtension
{
private static readonly string[] VietnameseSigns = new string[]
{
"aAeEoOuUiIdDyY",
"áàạảãâấầậẩẫăắằặẳẵ",
"ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
"éèẹẻẽêếềệểễ",
"ÉÈẸẺẼÊẾỀỆỂỄ",
"óòọỏõôốồộổỗơớờợởỡ",
"ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
"úùụủũưứừựửữ",
"ÚÙỤỦŨƯỨỪỰỬỮ",
"íìịỉĩ",
"ÍÌỊỈĨ",
"đ",
"Đ",
"ýỳỵỷỹ",
"ÝỲỴỶỸ"
};
public static string RemoveAccents(this string text)
{
if (String.IsNullOrEmpty(text))
{
return string.Empty;
}
for (int i = 1; i < VietnameseSigns.Length; i++)
{
for (int j = 0; j < VietnameseSigns[i].Length; j++)
text = text.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);
}
return text;
}
public static string Standardizing(this string text)
{
if (String.IsNullOrEmpty(text))
{
return string.Empty;
}
return text.RemoveAccents().ToLower();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment