-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from Thomas-Valkenburg/dev
Dev
- Loading branch information
Showing
26 changed files
with
2,092 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,68 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Internal; | ||
using Talpa_Api.Models; | ||
using Talpa_Api.Contexts; | ||
namespace Talpa_Api.Algorithms; | ||
|
||
namespace Talpa_Api.Algorithms | ||
public abstract class SimilarityCheck | ||
{ | ||
public abstract class SimilarityCheck | ||
{ | ||
public readonly struct ObjectWithSimilarity(int suggestionId, string title, double similarity) | ||
{ | ||
public int Id { get; init; } = suggestionId; | ||
public string Title { get; init; } = title; | ||
public double Similarity { get; init; } = similarity; | ||
} | ||
public readonly struct ObjectWithSimilarity(int suggestionId, string title, double similarity) | ||
{ | ||
public int Id { get; init; } = suggestionId; | ||
public string Title { get; init; } = title; | ||
public double Similarity { get; init; } = similarity; | ||
} | ||
|
||
private static double CalculateSimilarityPercentage(string string1, string string2) | ||
{ | ||
var pairs1 = WordLetterPairs(string1.ToUpper()); | ||
var pairs2 = WordLetterPairs(string2.ToUpper()); | ||
private static double CalculateSimilarityPercentage(string string1, string string2) | ||
{ | ||
var pairs1 = WordLetterPairs(string1.ToUpper()); | ||
var pairs2 = WordLetterPairs(string2.ToUpper()); | ||
|
||
var intersection = 0; | ||
var union = pairs1.Count + pairs2.Count; | ||
var intersection = 0; | ||
var union = pairs1.Count + pairs2.Count; | ||
|
||
foreach (var pair1 in pairs1) | ||
{ | ||
for (var number = 0; number < pairs2.Count; number++) | ||
{ | ||
if (pair1 != pairs2[number]) continue; | ||
foreach (var pair1 in pairs1) | ||
{ | ||
for (var number = 0; number < pairs2.Count; number++) | ||
{ | ||
if (pair1 != pairs2[number]) continue; | ||
|
||
intersection++; | ||
pairs2.RemoveAt(number); | ||
break; | ||
} | ||
} | ||
intersection++; | ||
pairs2.RemoveAt(number); | ||
break; | ||
} | ||
} | ||
|
||
// return the percentage of similarity | ||
return 2.0 * intersection * 100 / union; | ||
} | ||
// return the percentage of similarity | ||
return 2.0 * intersection * 100 / union; | ||
} | ||
|
||
private static List<string> WordLetterPairs(string str) | ||
{ | ||
var allPairs = new List<string>(); | ||
var words = str.Split(' '); | ||
private static List<string> WordLetterPairs(string str) | ||
{ | ||
var allPairs = new List<string>(); | ||
var words = str.Split(' '); | ||
|
||
foreach (var word in words) | ||
{ | ||
for (var number = 0; number < word.Length - 1; number++) | ||
{ | ||
allPairs.Add(word.Substring(number, 2)); | ||
} | ||
} | ||
foreach (var word in words) | ||
{ | ||
for (var number = 0; number < word.Length - 1; number++) | ||
{ | ||
allPairs.Add(word.Substring(number, 2)); | ||
} | ||
} | ||
|
||
return allPairs; | ||
} | ||
return allPairs; | ||
} | ||
|
||
public static (List<ObjectWithSimilarity> objects, double max) GetObjectWithSimilarity(string title, dynamic dbSet) | ||
{ | ||
var objectWithSimilarity = new List<ObjectWithSimilarity>(); | ||
var maxSimilarity = 0.0; | ||
public static (List<ObjectWithSimilarity> objects, double max) GetObjectWithSimilarity(string title, dynamic dbSet) | ||
{ | ||
var objectWithSimilarity = new List<ObjectWithSimilarity>(); | ||
var maxSimilarity = 0.0; | ||
|
||
foreach (var obj in dbSet) | ||
{ | ||
var sim = CalculateSimilarityPercentage(obj.Title, title); | ||
foreach (var obj in dbSet) | ||
{ | ||
var sim = CalculateSimilarityPercentage(obj.Title, title); | ||
|
||
if (sim > maxSimilarity) maxSimilarity = sim; | ||
if (sim > maxSimilarity) maxSimilarity = sim; | ||
|
||
if (sim > 50) objectWithSimilarity.Add(new ObjectWithSimilarity(obj.Id, obj.Title, sim)); | ||
} | ||
if (sim > 50) objectWithSimilarity.Add(new ObjectWithSimilarity(obj.Id, obj.Title, sim)); | ||
} | ||
|
||
return (objectWithSimilarity, maxSimilarity); | ||
} | ||
} | ||
return (objectWithSimilarity, maxSimilarity); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Talpa_Api.Contexts; | ||
using Talpa_Api.Models; | ||
|
||
namespace Talpa_Api.Controllers.Api; | ||
|
||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class CustomizationController(Context context) : ControllerBase | ||
{ | ||
private readonly List<string> AllowedExtensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"]; | ||
|
||
[HttpGet] | ||
public IActionResult GetCustomization() | ||
{ | ||
return Ok(context.Customization.FirstOrDefault()); | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult UpdateCustomization(string? name, bool? gradient, string? color1, string? color2, string? color3, | ||
IFormFile? image) | ||
{ | ||
var currentCustomization = context.Customization.FirstOrDefault(); | ||
|
||
if (image is not null) | ||
{ | ||
if (!AllowedExtensions.Contains(Path.GetExtension(image.FileName).ToLowerInvariant())) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
var path = Path.Combine("wwwroot", "logo", image.FileName); | ||
|
||
try | ||
{ | ||
if (!Directory.Exists(Path.Combine("wwwroot", "logo"))) Directory.CreateDirectory(Path.Combine("wwwroot", "logo")); | ||
|
||
var stream = new FileStream(path, FileMode.Create); | ||
|
||
image.CopyTo(stream); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return StatusCode(503, "Image write unavailable: " + ex); | ||
} | ||
} | ||
|
||
if (currentCustomization is null) | ||
{ | ||
if (!gradient.HasValue || color1 is null || image is null || | ||
(gradient is true && (color2 is null || color3 is null))) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
var customization = new Customization(name ?? "Talpa", gradient!.Value, color1, color2, color3, Path.Combine(HttpContext.Request.Host.Value, "logo", image.FileName)); | ||
|
||
context.Customization.Add(customization); | ||
context.SaveChanges(); | ||
|
||
return Created(); | ||
} | ||
|
||
if (name is not null) | ||
{ | ||
currentCustomization.Name = name; | ||
} | ||
|
||
if (gradient is not null) | ||
{ | ||
currentCustomization.Gradient = gradient.Value; | ||
} | ||
|
||
if (color1 is not null) | ||
{ | ||
currentCustomization.Color1 = color1; | ||
} | ||
|
||
if (color2 is not null) | ||
{ | ||
currentCustomization.Color2 = color2; | ||
} | ||
|
||
if (color3 is not null) | ||
{ | ||
currentCustomization.Color3 = color3; | ||
} | ||
|
||
if (image is not null) | ||
{ | ||
currentCustomization.LogoPath = Path.Combine(HttpContext.Request.Host.Value, "logo", image.FileName); | ||
} | ||
|
||
context.Customization.Update(currentCustomization); | ||
context.SaveChanges(); | ||
|
||
return NoContent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.