Skip to content

Commit

Permalink
Make tags nullable and check for null
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Valkenburg committed Jun 17, 2024
1 parent d7a5316 commit 8da649b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Talpa Api/Controllers/Api/SuggestionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SuggestionsController(Context context, IStringLocalizer<Localizatio
{
public class SuggestionData
{
public List<int> Tags { get; set; }
public List<int>? Tags { get; set; }
public IFormFile? Image { get; set; }
}

Expand Down Expand Up @@ -55,10 +55,15 @@ public async Task<ActionResult<List<Suggestion>>> GetSuggestions()
if (!overrideSimilarity && objects.Count > 0 && max > 70)
return Accepted(objects.OrderByDescending(x => x.Similarity).ToList());

if (data.Tags.Any(tag => context.Tags.Find(tag) is null))
return NotFound(localizer["TagNotFound"].Value);
List<Tag> tags = [];

var tags = await context.Tags.Where(tag => data.Tags.Contains(tag.Id)).ToListAsync();
if (data.Tags is not null)
{
if (data.Tags.Any(tag => context.Tags.Find(tag) is null))
return NotFound(localizer["TagNotFound"].Value);

tags = await context.Tags.Where(tag => data.Tags.Contains(tag.Id)).ToListAsync();
}

context.Suggestions.Add(new Suggestion
{
Expand Down

0 comments on commit 8da649b

Please sign in to comment.