Skip to content

Commit

Permalink
Merge pull request #192 from learntocloud/adding-tests
Browse files Browse the repository at this point in the history
Added get project by word function
  • Loading branch information
madebygps authored Feb 27, 2024
2 parents 0ac4b7b + de4216a commit 5e3ae0a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/DictionaryFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ public async Task<HttpResponseData> GetDefinitionByWord(
return CreateJsonResponse(req, HttpStatusCode.OK, definition);
}

[Function("GetProjectByWord")]
public async Task<HttpResponseData> GetProjectByWord(
[HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req, string word)
{
var project = await _definitionsRepository.GetProjectByWordAsync(word);
if (project == null)
{
_logger.LogInformation($"No project found for word: {word}");
return CreateJsonResponse(req, HttpStatusCode.NotFound, new { Error = $"No project found for word {word}." });
}
_logger.LogInformation($"Definition retrieved for word: {word}");
return CreateJsonResponse(req, HttpStatusCode.OK, project);
}

[Function("GetDefinitionsByTag")]
public async Task<HttpResponseData> GetDefinitionsByTagAsync(
[HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req, string tag, string? continuationToken = null, int? pageSize = 10)
Expand Down
24 changes: 21 additions & 3 deletions src/Repositories/DefinitionsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ namespace cloud_dictionary
public class DefinitionsRepository
{
private readonly Container _definitionsCollection;
private readonly Container _projectsCollection;
private const int MaxPageSize = 50;
private static readonly Random random = new();
public DefinitionsRepository(CosmosClient client, IConfiguration configuration)
{
var database = client.GetDatabase(configuration["AZURE_COSMOS_DATABASE_NAME"]);
_definitionsCollection = database.GetContainer(configuration["AZURE_COSMOS_CONTAINER_NAME"]);
_projectsCollection = database.GetContainer(configuration["AZURE_COSMOS_PROJECT_CONTAINER_NAME"]);
}
public async Task<(IEnumerable<Definition>, string?)> GetAllDefinitionsAsync(int? pageSize, string? continuationToken)
{
Expand All @@ -29,10 +31,26 @@ public DefinitionsRepository(CosmosClient client, IConfiguration configuration)
var response = await _definitionsCollection.ReadItemAsync<Definition>(id, new PartitionKey(word));
return response.Resource;
}

public async Task<Project?> GetProjectByWordAsync(string word)
{
var queryDefinition = new QueryDefinition("SELECT * FROM Project d WHERE LOWER(d.word) = @word").WithParameter("@word", word.ToLower());
var queryResultSetIterator = _projectsCollection.GetItemQueryIterator<Project>(queryDefinition);
List<Project> projects = new();
while (queryResultSetIterator.HasMoreResults)
{
FeedResponse<Project> currentResultSet = await queryResultSetIterator.ReadNextAsync();
foreach (Project project in currentResultSet)
{
projects.Add(project);
}
}
return projects.FirstOrDefault();
}
public async Task<Definition?> GetDefinitionByWordAsync(string word)
{

var queryDefinition = new QueryDefinition("SELECT * FROM Definitions d WHERE LOWER(d.word) = @word").WithParameter("@word", word.ToLower());
var queryDefinition = new QueryDefinition("SELECT * FROM Definition d WHERE LOWER(d.word) = @word").WithParameter("@word", word.ToLower());
var queryResultSetIterator = _definitionsCollection.GetItemQueryIterator<Definition>(queryDefinition);
List<Definition> definitions = new();

Expand All @@ -48,7 +66,7 @@ public DefinitionsRepository(CosmosClient client, IConfiguration configuration)
}
public async Task<(IEnumerable<Definition>, string?)> GetDefinitionsByTagAsync(string tag, int? pageSize, string? continuationToken)
{
var query = $"SELECT * FROM Definitions d WHERE LOWER(d.tag) = '{tag.ToLower()}'";
var query = $"SELECT * FROM Definition d WHERE LOWER(d.tag) = '{tag.ToLower()}'";
pageSize = pageSize.HasValue && pageSize.Value <= MaxPageSize ? pageSize.Value : MaxPageSize;
return await QueryWithPagingAsync<Definition>(query, pageSize, continuationToken);
}
Expand Down Expand Up @@ -104,7 +122,7 @@ public async Task UpdateDefinition(Definition existingDefinition)
public async Task<Definition?> GetRandomDefinitionAsync()
{
int count = await GetDefinitionCountAsync();
// Use DefinitionService.cs to get the count of definitions


int randomIndex = random.Next(0, count);
var query = _definitionsCollection.GetItemLinqQueryable<Definition>()
Expand Down
16 changes: 16 additions & 0 deletions src/Shared/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;

namespace cloud_dictionary.Shared
{
public class Project
{
public Project() { }

Check warning on line 7 in src/Shared/Project.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Non-nullable property 'Word' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 7 in src/Shared/Project.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Non-nullable property 'Description' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public string? Id { get; set; }

[Required]
public string Word { get; set; }
[Required]
public string Description { get; set; }
}
}

0 comments on commit 5e3ae0a

Please sign in to comment.