Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update post and put routes #78

Merged
merged 7 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/UDS.Net.API.Client/AuthenticatedClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace UDS.Net.API.Client
{
Expand Down Expand Up @@ -43,7 +41,7 @@
// _StudiesBaseAddress = configuration["DownstreamApis:StudiesApi:Url"];
//}

protected async Task PrepareAuthenticatedClient()

Check warning on line 44 in src/UDS.Net.API.Client/AuthenticatedClient.cs

View workflow job for this annotation

GitHub Actions / Package and publish

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
//var accessToken = await _tokenAcquisition.GetAccessTokenForUserAsync(new[] { _ApiScope });
//_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
Expand Down Expand Up @@ -126,20 +124,24 @@
return value;
}

public async Task Post(T dto)
public async Task<T> Post(T dto)
{
var json = JsonSerializer.Serialize(dto);

string response = await PostRequest(_BasePath, json);

/// TODO how to handle failures?

return JsonSerializer.Deserialize<T>(response, options);
}

public async Task Put(int id, T dto)
public async Task<T> Put(int id, T dto)
{
var json = JsonSerializer.Serialize(dto);

string response = await PutRequest($"{_BasePath}/{id}", json);
var response = await PutRequest($"{_BasePath}/{id}", json);

return JsonSerializer.Deserialize<T>(response, options);
}

public Task Delete(int id)
Expand Down
4 changes: 2 additions & 2 deletions src/UDS.Net.API.Client/IBaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public interface IBaseClient<T>
Task<IEnumerable<T>> Get(int pageSize = 10, int pageIndex = 1);
Task<int> Count();
Task<T> Get(int id);
Task Post(T dto);
Task Put(int id, T dto);
Task<T> Post(T dto);
Task<T> Put(int id, T dto);
Task Delete(int id);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/UDS.Net.API.Client/UDS.Net.API.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
<PackageId>UDS.Net.API.Client</PackageId>
<Version>4.2.0</Version>
<Version>4.3.0</Version>
<Authors>Sanders-Brown Center on Aging</Authors>
<Description>UDS client library for using UDS.Net.API</Description>
<Owners>UK-SBCoA</Owners>
<PackageDescription>Client library for API</PackageDescription>
<RepositoryUrl>https://github.com/UK-SBCoA/uniform-data-set-dotnet-api</RepositoryUrl>
<ReleaseVersion>4.2.0</ReleaseVersion>
<ReleaseVersion>4.3.0</ReleaseVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.5" />
Expand Down
8 changes: 3 additions & 5 deletions src/UDS.Net.API/Controllers/FormsController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using UDS.Net.API.Client;
using UDS.Net.API.Data;
using UDS.Net.API.Entities;
using UDS.Net.API.Extensions;
using UDS.Net.Dto;

Expand Down Expand Up @@ -373,14 +371,14 @@ public Task<FormDto> Get(int id)

[Obsolete]
[HttpPost]
public Task Post(FormDto dto)
public Task<FormDto> Post(FormDto dto)
{
throw new NotImplementedException();
}

[Obsolete]
[HttpPut("{id}")]
public Task Put(int id, FormDto dto)
public Task<FormDto> Put(int id, FormDto dto)
{
throw new NotImplementedException();
}
Expand Down
11 changes: 3 additions & 8 deletions src/UDS.Net.API/Controllers/LookupsController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using UDS.Net.API.Client;
using UDS.Net.API.Data;
Expand Down Expand Up @@ -106,13 +101,13 @@ public Task<LookupDto> Get(int id)
}

[HttpPost]
public Task Post(LookupDto dto)
public Task<LookupDto> Post(LookupDto dto)
{
throw new NotImplementedException();
}

[HttpPut("{id}")]
public Task Put(int id, LookupDto dto)
public Task<LookupDto> Put(int id, LookupDto dto)
{
throw new NotImplementedException();
}
Expand Down
9 changes: 6 additions & 3 deletions src/UDS.Net.API/Controllers/PacketsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.EntityFrameworkCore;
using UDS.Net.API.Client;
using UDS.Net.API.Data;
using UDS.Net.API.Entities;
using UDS.Net.API.Extensions;
using UDS.Net.Dto;

Expand Down Expand Up @@ -134,7 +133,7 @@ public async Task<PacketDto> GetPacketWithForms(int id)
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
[HttpPost]
public async Task Post(PacketDto dto)
public async Task<PacketDto> Post(PacketDto dto)
{
throw new NotImplementedException("Packets are not created, use Visits.");
}
Expand All @@ -146,7 +145,7 @@ public async Task Post(PacketDto dto)
/// <param name="dto"></param>
/// <returns></returns>
[HttpPut("{id}")]
public async Task Put(int id, [FromBody] PacketDto dto)
public async Task<PacketDto> Put(int id, [FromBody] PacketDto dto)
{
if (dto != null)
{
Expand Down Expand Up @@ -211,8 +210,12 @@ public async Task Put(int id, [FromBody] PacketDto dto)

_context.Visits.Update(existingPacket);
await _context.SaveChangesAsync();

return existingPacket.ToPacketDto();
}
}

return dto;
}

[HttpDelete("{id}")]
Expand Down
15 changes: 8 additions & 7 deletions src/UDS.Net.API/Controllers/ParticipationsController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using UDS.Net.API.Client;
Expand Down Expand Up @@ -51,13 +47,13 @@ public async Task<ParticipationDto> Get(int id)
}

[HttpPost]
public async Task Post([FromBody] ParticipationDto dto)
public async Task<ParticipationDto> Post([FromBody] ParticipationDto dto)
{
var existingLegacyid = await _context.Participations.AnyAsync(p => p.LegacyId == dto.LegacyId);

if (existingLegacyid)
{
return;
return dto;

}
else
Expand All @@ -73,10 +69,12 @@ public async Task Post([FromBody] ParticipationDto dto)
};
_context.Participations.Add(newParticipation);
await _context.SaveChangesAsync();

return newParticipation.ToDto();
}
}
[HttpPut("{id}")]
public async Task Put(int id, [FromBody] ParticipationDto dto)
public async Task<ParticipationDto> Put(int id, [FromBody] ParticipationDto dto)
{
var participation = await _context.Participations.FindAsync(id);

Expand All @@ -91,8 +89,11 @@ public async Task Put(int id, [FromBody] ParticipationDto dto)

_context.Participations.Update(participation);
await _context.SaveChangesAsync();

return participation.ToDto();
}

return dto;
}

[HttpDelete("{id}")]
Expand Down
9 changes: 6 additions & 3 deletions src/UDS.Net.API/Controllers/VisitsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,14 @@ public async Task<VisitDto> GetWithForm(int id, string formKind)
}

[HttpPost]
public async Task Post([FromBody] VisitDto dto)
public async Task<VisitDto> Post([FromBody] VisitDto dto)
{
var visit = dto.Convert();

_context.Visits.Add(visit);
await _context.SaveChangesAsync();

return visit.ToDto();
}

private string GetFormKind(VisitDto dto)
Expand Down Expand Up @@ -581,7 +583,7 @@ public async Task PostWithForm(int id, string formKind, [FromBody] VisitDto dto)
}

[HttpPut("{id}")]
public async Task Put(int id, [FromBody] VisitDto dto)
public async Task<VisitDto> Put(int id, [FromBody] VisitDto dto)
{
// check to see if the dto has a full form
string formKind = GetFormKind(dto);
Expand Down Expand Up @@ -614,9 +616,10 @@ public async Task Put(int id, [FromBody] VisitDto dto)
_context.Visits.Update(visit);
await _context.SaveChangesAsync();

return;
return visit.ToDto();
}

return dto;
}

[HttpDelete("{id}")]
Expand Down
2 changes: 1 addition & 1 deletion src/UDS.Net.API/UDS.Net.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<ReleaseVersion>4.2.0</ReleaseVersion>
<ReleaseVersion>4.3.0</ReleaseVersion>
<DockerComposeProjectPath>../docker-compose.dcproj</DockerComposeProjectPath>
<UserSecretsId>c1dd1715-6fa0-4515-bcf2-6a7f6a0c11a5</UserSecretsId>
<Configurations>Release;Debug</Configurations>
Expand Down
4 changes: 2 additions & 2 deletions src/UDS.Net.Dto/UDS.Net.Dto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
<PackageId>UDS.Net.Dto</PackageId>
<Version>4.2.0</Version>
<Version>4.3.0</Version>
<Authors>Sanders-Brown Center on Aging</Authors>
<Description>UDS data transfer objects for use with API</Description>
<Owners>UK-SBCoA</Owners>
<PackageDescription>Dtos for API</PackageDescription>
<RepositoryUrl>https://github.com/UK-SBCoA/uniform-data-set-dotnet-api</RepositoryUrl>
<ReleaseVersion>4.2.0</ReleaseVersion>
<ReleaseVersion>4.3.0</ReleaseVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.5" />
Expand Down
Loading