-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔧 feat(membership-service): Change membership service to use simple A…
…DO .NET, adapt REST endpoints
- Loading branch information
1 parent
11dc18a
commit ace7b49
Showing
10 changed files
with
189 additions
and
337 deletions.
There are no files selected for viewing
153 changes: 105 additions & 48 deletions
153
src/membership-service/Controllers/MembershipController.cs
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,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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.