Skip to content

Commit

Permalink
🔧 feat(membership-service): Change membership service to use simple A…
Browse files Browse the repository at this point in the history
…DO .NET, adapt REST endpoints
  • Loading branch information
orazefabian committed May 13, 2024
1 parent 11dc18a commit ace7b49
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 337 deletions.
153 changes: 105 additions & 48 deletions src/membership-service/Controllers/MembershipController.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1,122 @@
// <copyright company="Dynatrace LLC">
// Copyright 2023 Dynatrace LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Data;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MembershipService.Persistence;
using MySqlConnector;

namespace MembershipService.Controllers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;

[ApiController]
[Route("[controller]-service")]
public class MembershipController : ControllerBase
namespace MembershipService.Controllers
{

[ApiController]
[Route("[controller]-service")]
public class MembershipController : ControllerBase
{
private readonly string _connectionString;
private readonly ILogger<MembershipController> _logger;
private readonly MembershipDbService _membershipDbService;

public MembershipController(ILogger<MembershipController> logger, MembershipDbService membershipDbService)
public MembershipController(IConfiguration configuration, ILogger<MembershipController> logger)
{
_logger = logger;
_membershipDbService = membershipDbService;
string dbPwd = Environment.GetEnvironmentVariable("MARIADB_PASSWORD");
string dbHost = Environment.GetEnvironmentVariable("MARIADB_SERVICE");
_connectionString = $"Server={dbHost};Port=3306;Database=memberships;user=root;password={dbPwd}";
_logger = logger;
}

[HttpGet("{userid}", Name = "FindOne")]
public async Task<ActionResult<MembershipEntity>> Get(int userid)
[HttpGet("{userid}")]
public async Task<IActionResult> GetMembershipStatus(string userid)
{
var result = await _membershipDbService.FindOne(userid);
if (result == default)
using (var connection = new MySqlConnection(_connectionString))
{
result = new MembershipEntity(userid, "FREE");
_logger.LogInformation($"GET requested => userid: {userid}");

await connection.OpenAsync();

string selectQuery = "SELECT membership FROM membership WHERE userid = " + userid;


_logger.LogInformation($"Executing query: {selectQuery}");

using (var selectCmd = new MySqlCommand(selectQuery, connection))
{
try
{
object result = await selectCmd.ExecuteScalarAsync();
if (result != null)
{
_logger.LogInformation($@"Membership status of userid {userid}: {result}");
return Ok(result);
}
else
{
_logger.LogInformation($"Membership status of userid {userid} not found");

// If no membership entry exists, insert a new one
string insertQuery = $@"INSERT INTO membership (userid, membership) VALUES ({userid}, ""FREE"")";
using (var insertCmd = new MySqlCommand(insertQuery, connection))
{
int rowsAffected = await insertCmd.ExecuteNonQueryAsync();
if (rowsAffected > 0)
{
_logger.LogInformation($"Membership status for userid {userid} inserted as 'FREE'");
return Ok("FREE");
}
else
{
_logger.LogInformation($"Membership status for userid {userid} could not be inserted");
return StatusCode((int)HttpStatusCode.InternalServerError);
}
}
}
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
}
}
}
return Ok(result);
}

[HttpPost]
public async Task<ActionResult<MembershipEntity>> Insert(MembershipEntity dto)

[HttpPost("add/{userid}")]
public async Task<IActionResult> SetMembershipStatus(string userid, [FromForm(Name = "membership")] string membership)
{
try
using (var connection = new MySqlConnection(_connectionString))
{
var id = await _membershipDbService.Insert(dto);
if (id != null)
return id;
else
return BadRequest();
}
catch(MySqlException e)
{
_logger.LogError(0, e, "Error while inserting membership");
return StatusCode(500);
await connection.OpenAsync();

_logger.LogInformation($"INSERT requested => userid: {userid}, membership: {membership}");

string query = "INSERT INTO membership (userid, membership) VALUES (" + userid + ",\"" + membership + "\") ON DUPLICATE KEY UPDATE membership = \"" + membership + "\"";


_logger.LogInformation($"Executing query: {query}");


using (var cmd = new MySqlCommand(query, connection))
{
try
{
int rowsAffected = await cmd.ExecuteNonQueryAsync();
if (rowsAffected > 0)
{
_logger.LogInformation($"Membership status for userid {userid} updated to {membership}");
return Ok();
}
else
{
_logger.LogInformation($"Membership status for userid {userid} could not be updated");
return StatusCode((int)HttpStatusCode.InternalServerError);
}
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
}
}
}
}
}
}
}
27 changes: 0 additions & 27 deletions src/membership-service/Membership.cs

This file was deleted.

8 changes: 5 additions & 3 deletions src/membership-service/MembershipService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.20" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="MySql.Data" Version="8.0.28" />
</ItemGroup>

</Project>
29 changes: 0 additions & 29 deletions src/membership-service/Persistence/DbInitializer.cs

This file was deleted.

36 changes: 0 additions & 36 deletions src/membership-service/Persistence/MariaDbContext.cs

This file was deleted.

59 changes: 0 additions & 59 deletions src/membership-service/Persistence/MembershipDbService.cs

This file was deleted.

40 changes: 0 additions & 40 deletions src/membership-service/Persistence/MembershipEntity.cs

This file was deleted.

Loading

0 comments on commit ace7b49

Please sign in to comment.