working with dummy db
This commit is contained in:
parent
9e61c50958
commit
61484bff16
|
@ -1,10 +1,12 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
using BlueWest.Collections;
|
using BlueWest.Collections;
|
||||||
using BlueWest.Data;
|
using BlueWest.Data;
|
||||||
|
using BlueWest.WebApi.MySQL;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using PerformanceSolution.Data;
|
|
||||||
|
|
||||||
namespace BlueWest.WebApi.Controllers
|
namespace BlueWest.WebApi.Controllers
|
||||||
{
|
{
|
||||||
|
@ -12,29 +14,33 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class UserController : ControllerBase
|
public class UserController : ControllerBase
|
||||||
{
|
{
|
||||||
private UserList _userList => MemoryData.UserList;
|
|
||||||
|
private readonly MysqlDbContext _dbContext;
|
||||||
|
|
||||||
|
public UserController(MysqlDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
}
|
||||||
|
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public ActionResult Get()
|
public ActionResult Get()
|
||||||
{
|
{
|
||||||
if (_userList != null)
|
var users = _dbContext.Users.ToImmutableArray();
|
||||||
{
|
|
||||||
return Ok(_userList.Users.Values.ToImmutableArray());
|
return Ok(users);
|
||||||
}
|
|
||||||
|
|
||||||
return new NotFoundResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
[HttpGet("{userId}", Name = nameof(GetUserById))]
|
[HttpGet("{userId}", Name = nameof(GetUserById))]
|
||||||
public ActionResult GetUserById(int userId)
|
public ActionResult GetUserById(TimeSpan userId)
|
||||||
{
|
{
|
||||||
var user = MemoryData.GetUserById(userId);
|
var user = _dbContext.Users.FirstOrDefault(x => x.Id == userId);
|
||||||
|
|
||||||
if (user.Id != TimeSpan.Zero)
|
if (user != null)
|
||||||
{
|
{
|
||||||
return Ok(user);
|
return Ok(user);
|
||||||
}
|
}
|
||||||
|
@ -48,8 +54,7 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[HttpGet("{userId}/transactions")]
|
[HttpGet("{userId}/transactions")]
|
||||||
public ActionResult GetTransactions(int userId)
|
public ActionResult GetTransactions(int userId)
|
||||||
{
|
{
|
||||||
var transactions = MemoryData.GetUserTransactions(userId);
|
return Ok();
|
||||||
return Ok(transactions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -58,28 +63,17 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[HttpGet("{userId}/transactions/{transactionId}")]
|
[HttpGet("{userId}/transactions/{transactionId}")]
|
||||||
public ActionResult GetTransactionsById(int userId, TimeSpan transactionId)
|
public ActionResult GetTransactionsById(int userId, TimeSpan transactionId)
|
||||||
{
|
{
|
||||||
var user = MemoryData.UserList.GetUserById(userId);
|
|
||||||
|
|
||||||
if(user.Id == TimeSpan.Zero) return new NotFoundResult();
|
|
||||||
|
|
||||||
var transactions = user.FinanceTransactions;
|
|
||||||
|
|
||||||
FinanceTransaction? transaction = transactions?.Values.ToImmutableArray().FirstOrNull(t => t.CreationDate == transactionId);
|
|
||||||
|
|
||||||
if (transaction != null)
|
|
||||||
{
|
|
||||||
return Ok(transaction);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new NotFoundResult();
|
return new NotFoundResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult AddOrModifyUser(UserUpdateDto userUpdateDto)
|
public ActionResult AddUser(UserUpdateDto userUpdateDto)
|
||||||
{
|
{
|
||||||
var user = MemoryData.AddOrModifyUser(userUpdateDto);
|
var user = new User(userUpdateDto, DateTime.Now.TimeOfDay, new List<FinanceTransaction>());
|
||||||
|
_dbContext.Users.Add(user);
|
||||||
|
_dbContext.SaveChanges();
|
||||||
return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user);
|
return CreatedAtRoute(nameof(GetUserById), new {userId = user.Id}, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,26 +82,23 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[HttpPut("{userId}")]
|
[HttpPut("{userId}")]
|
||||||
public ActionResult UpdateUser(int userId, UserUpdateDto userUpdate)
|
public ActionResult UpdateUser(int userId, UserUpdateDto userUpdate)
|
||||||
{
|
{
|
||||||
var result = MemoryData.UserList.UpdateUser(userId, userUpdate);
|
|
||||||
|
|
||||||
if (result != null) return Ok(result);
|
return new NotFoundResult();
|
||||||
|
|
||||||
return BadRequest();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
public ActionResult DeleteUser(int id)
|
public ActionResult DeleteUser(TimeSpan id)
|
||||||
{
|
{
|
||||||
bool result = MemoryData.UserList.RemoveUser(id);
|
var user = _dbContext.Users.FirstOrDefault(u => u.Id == id);
|
||||||
|
if (user == null)
|
||||||
if (result)
|
|
||||||
{
|
{
|
||||||
return NoContent();
|
return new NotFoundResult();
|
||||||
}
|
}
|
||||||
|
_dbContext.Users.Remove(user);
|
||||||
return new BadRequestResult();
|
_dbContext.SaveChanges();
|
||||||
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
@ -115,10 +106,6 @@ namespace BlueWest.WebApi.Controllers
|
||||||
[HttpPost("{userId}/transactions")]
|
[HttpPost("{userId}/transactions")]
|
||||||
public ActionResult PostTransaction(int userId, FinanceTransactionInsertDto financeTransaction)
|
public ActionResult PostTransaction(int userId, FinanceTransactionInsertDto financeTransaction)
|
||||||
{
|
{
|
||||||
FinanceTransaction result = MemoryData.UserList.AddFinanceTransaction(userId, financeTransaction);
|
|
||||||
|
|
||||||
if(result.UserId != TimeSpan.Zero) return Ok(result);
|
|
||||||
|
|
||||||
return new BadRequestResult();
|
return new BadRequestResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
using System;
|
||||||
|
using BlueWest.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
namespace BlueWest.WebApi.MySQL;
|
||||||
|
|
||||||
|
public sealed class MysqlDbContext : DbContext
|
||||||
|
{
|
||||||
|
public DbSet<User> Users { get; set; }
|
||||||
|
public DbSet<FinanceTransaction> Transactions { get; set; }
|
||||||
|
public IConfiguration Configuration;
|
||||||
|
|
||||||
|
|
||||||
|
public MysqlDbContext(DbContextOptions<MysqlDbContext> options) : base(options)
|
||||||
|
{
|
||||||
|
Database.EnsureCreated();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
|
{
|
||||||
|
/*optionsBuilder.UseMySql(
|
||||||
|
Configuration.GetConnectionString("LocalMySQL"),
|
||||||
|
new MySqlServerVersion(new Version(8, 0, 11))
|
||||||
|
);*/
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
modelBuilder.Entity<User>(builder =>
|
||||||
|
{
|
||||||
|
builder.HasKey(x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity<FinanceTransaction>(builder =>
|
||||||
|
{
|
||||||
|
builder.HasOne<User>()
|
||||||
|
.WithMany(x => x.FinanceTransactions)
|
||||||
|
.HasForeignKey(x => x.UserId);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,8 +10,10 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using BlueWest.WebApi.MySQL;
|
||||||
using BlueWest.WebApi.Service;
|
using BlueWest.WebApi.Service;
|
||||||
using BlueWest.WebApi.Tools;
|
using BlueWest.WebApi.Tools;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
|
@ -57,7 +59,8 @@ namespace BlueWest.WebApi
|
||||||
Version = "v1"
|
Version = "v1"
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
services.AddDbContextPool<MysqlDbContext>(options =>
|
||||||
|
options.UseMySql(Configuration.GetConnectionString("LocalMySQL"), new MySqlServerVersion(new Version(8, 0, 11))));
|
||||||
services.AddGrpc();
|
services.AddGrpc();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace BlueWest.WebApi
|
|
||||||
{
|
|
||||||
public class WeatherForecast
|
|
||||||
{
|
|
||||||
public DateTime Date { get; set; }
|
|
||||||
|
|
||||||
public int TemperatureC { get; set; }
|
|
||||||
|
|
||||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
|
||||||
|
|
||||||
public string Summary { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -6,5 +6,8 @@
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*",
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"LocalMySQL": "server=127.0.0.1;user=blueuser;password=JwuWxhWxhh$X1;database=bluedb;"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,10 +23,24 @@ namespace BlueWest.Collections
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TSource? FirstOrNull<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TSource : struct =>
|
public static TSource FirstOrNullRef<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TSource : class =>
|
||||||
source.TryGetFirst(predicate);
|
source.TryGetFirstClass(predicate);
|
||||||
|
|
||||||
private static TSource? TryGetFirst<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TSource : struct
|
public static TSource? FirstOrNullStruct<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TSource : struct =>
|
||||||
|
source.TryGetFirstStruct(predicate);
|
||||||
|
|
||||||
|
private static TSource? TryGetFirstStruct<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TSource : struct
|
||||||
|
{
|
||||||
|
foreach (TSource obj in source)
|
||||||
|
{
|
||||||
|
if (predicate(obj))
|
||||||
|
{
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
private static TSource TryGetFirstClass<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) where TSource : class
|
||||||
{
|
{
|
||||||
foreach (TSource obj in source)
|
foreach (TSource obj in source)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
|
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
|
||||||
<RootNamespace>BlueWest.Data</RootNamespace>
|
<RootNamespace>BlueWest.Data</RootNamespace>
|
||||||
<AnalysisLevel>preview</AnalysisLevel>
|
<AnalysisLevel>preview</AnalysisLevel>
|
||||||
|
@ -9,7 +9,6 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MessagePack" Version="2.3.85" />
|
<PackageReference Include="MessagePack" Version="2.3.85" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="6.0.4" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace BlueWest.Data
|
|
||||||
{
|
|
||||||
public class DataObject
|
|
||||||
{
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return JsonConvert.SerializeObject(this, Formatting.Indented);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,24 +1,27 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using MapTo;
|
using MapTo;
|
||||||
using MessagePack;
|
|
||||||
|
|
||||||
namespace BlueWest.Data
|
namespace BlueWest.Data
|
||||||
{
|
{
|
||||||
|
|
||||||
[MessagePackObject]
|
|
||||||
[MapFrom(typeof(FinanceTransactionInsertDto))]
|
[MapFrom(typeof(FinanceTransactionInsertDto))]
|
||||||
public partial struct FinanceTransaction
|
public partial class FinanceTransaction
|
||||||
{
|
{
|
||||||
[Key(1)] public TimeSpan CreationDate { get; set; }
|
[Key] public TimeSpan CreationDate { get; set; }
|
||||||
[Key(2)] public TimeSpan UserId { get; set; }
|
public TimeSpan UserId { get; set; }
|
||||||
|
|
||||||
[Key(4)] public string Currency { get; }
|
public string Currency { get; }
|
||||||
[Key(5)] public double Amount { get; } // To Buy
|
public double Amount { get; } // To Buy
|
||||||
[Key(6)] public double Quantity { get; } // Bought
|
public double Quantity { get; } // Bought
|
||||||
[Key(7)] public double Fee { get; }
|
public double Fee { get; }
|
||||||
[Key(8)] public TimeSpan UploadedDate { get; }
|
public TimeSpan UploadedDate { get; }
|
||||||
[Key(9)] public TimeSpan TransactionDate { get; }
|
public TimeSpan TransactionDate { get; }
|
||||||
|
|
||||||
|
public FinanceTransaction()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public FinanceTransaction(TimeSpan creationDate, TimeSpan userId,
|
public FinanceTransaction(TimeSpan creationDate, TimeSpan userId,
|
||||||
string currency, double amount, double quantity, double fee, TimeSpan uploadedDate, TimeSpan transactionDate)
|
string currency, double amount, double quantity, double fee, TimeSpan uploadedDate, TimeSpan transactionDate)
|
||||||
|
|
|
@ -7,7 +7,7 @@ namespace BlueWest.Data
|
||||||
{
|
{
|
||||||
[MapFrom(typeof(FinanceTransaction))]
|
[MapFrom(typeof(FinanceTransaction))]
|
||||||
|
|
||||||
public partial struct FinanceTransactionInsertDto
|
public partial class FinanceTransactionInsertDto
|
||||||
{
|
{
|
||||||
public string Currency { get; }
|
public string Currency { get; }
|
||||||
public double Amount { get; } // To Buy
|
public double Amount { get; } // To Buy
|
||||||
|
|
|
@ -7,7 +7,7 @@ namespace BlueWest.Data
|
||||||
{
|
{
|
||||||
[MapFrom(typeof(FinanceTransaction))]
|
[MapFrom(typeof(FinanceTransaction))]
|
||||||
|
|
||||||
public partial struct FinanceTransactionReadDto
|
public partial class FinanceTransactionReadDto
|
||||||
{
|
{
|
||||||
public TimeSpan CreationDate { get; set; }
|
public TimeSpan CreationDate { get; set; }
|
||||||
public TimeSpan UserId { get; set; }
|
public TimeSpan UserId { get; set; }
|
||||||
|
|
|
@ -4,28 +4,24 @@ using System.Collections.Generic;
|
||||||
using System.Transactions;
|
using System.Transactions;
|
||||||
using BlueWest.Collections;
|
using BlueWest.Collections;
|
||||||
using MapTo;
|
using MapTo;
|
||||||
using MessagePack;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using BlueWest.Collections;
|
using BlueWest.Collections;
|
||||||
|
|
||||||
namespace BlueWest.Data
|
namespace BlueWest.Data
|
||||||
{
|
{
|
||||||
[MessagePackObject]
|
|
||||||
[UseUpdate]
|
[UseUpdate]
|
||||||
[MapFrom(typeof(UserUpdateDto))]
|
[MapFrom(typeof(UserUpdateDto))]
|
||||||
public partial class User
|
public partial class User
|
||||||
{
|
{
|
||||||
[Key(1)] public TimeSpan Id { get; } = TimeSpan.Zero;
|
public TimeSpan Id { get; } = TimeSpan.Zero;
|
||||||
[Key(2)] public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public List<FinanceTransaction> FinanceTransactions { get; }
|
||||||
|
|
||||||
|
|
||||||
[Key(8)] public FastDictionary<int, FinanceTransaction> FinanceTransactions { get; }
|
public User(TimeSpan id, string name)
|
||||||
|
|
||||||
public User(TimeSpan id, string name, FastDictionary<int, FinanceTransaction> financeTransactions)
|
|
||||||
{
|
{
|
||||||
Id = id;
|
Id = id;
|
||||||
Name = name;
|
Name = name;
|
||||||
FinanceTransactions = financeTransactions;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public User()
|
public User()
|
||||||
|
@ -33,25 +29,6 @@ namespace BlueWest.Data
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddTransaction(FinanceTransaction financeTransaction)
|
|
||||||
{
|
|
||||||
FinanceTransactions.Add(FinanceTransactions.Count + 1, financeTransaction);
|
|
||||||
}
|
|
||||||
public void AddTransaction(FinanceTransactionInsertDto financeTransaction)
|
|
||||||
{
|
|
||||||
FinanceTransactions.Add(FinanceTransactions.Count + 1,
|
|
||||||
new FinanceTransaction(financeTransaction, TimeSpan.FromTicks(DateTime.Now.Ticks), Id));
|
|
||||||
}
|
|
||||||
|
|
||||||
internal bool HasTransaction(int transactionId)
|
|
||||||
{
|
|
||||||
return FinanceTransactions.Contains(transactionId);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal FinanceTransaction GetTransactionById(int transactionId)
|
|
||||||
{
|
|
||||||
return FinanceTransactions[transactionId];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,6 @@ namespace BlueWest.Data
|
||||||
{
|
{
|
||||||
public static class UserExtensions
|
public static class UserExtensions
|
||||||
{
|
{
|
||||||
public static bool UserHasTransaction(this User user, int transactionId)
|
|
||||||
{
|
|
||||||
return user.FinanceTransactions.Contains(transactionId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,125 +1 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Immutable;
|
|
||||||
using System.Transactions;
|
|
||||||
using BlueWest.Collections;
|
|
||||||
using MessagePack;
|
|
||||||
|
|
||||||
namespace BlueWest.Data
|
|
||||||
{
|
|
||||||
[MessagePackObject]
|
|
||||||
public class UserList: DataObject
|
|
||||||
{
|
|
||||||
[Key(9)] public readonly FastDictionary<int, User> Users;
|
|
||||||
|
|
||||||
[IgnoreMember] public int Length = 0;
|
|
||||||
|
|
||||||
public UserList(FastDictionary<int, User> users)
|
|
||||||
{
|
|
||||||
Users = users;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int GetIndexById(int userId)
|
|
||||||
{
|
|
||||||
var keys = Users.Keys;
|
|
||||||
|
|
||||||
foreach (var key in keys)
|
|
||||||
{
|
|
||||||
if (key == userId) return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public User GetUserById(int id)
|
|
||||||
{
|
|
||||||
if (Users.ContainsKey(id)) return Users[id];
|
|
||||||
return new User();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool RemoveUser(int userId)
|
|
||||||
{
|
|
||||||
if (Users.ContainsKey(userId))
|
|
||||||
{
|
|
||||||
Users.Remove(userId);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal bool HasUser(int userId)
|
|
||||||
{
|
|
||||||
return Users.Contains(userId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public User UpdateUser(int userId, UserUpdateDto userUpdate)
|
|
||||||
{
|
|
||||||
if (Users.ContainsKey(userId))
|
|
||||||
{
|
|
||||||
Users[userId].Update(userUpdate);
|
|
||||||
return Users[userId];
|
|
||||||
}
|
|
||||||
|
|
||||||
return new User();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
internal bool UserHasTransaction(int userId, int transactionId) => HasUser(userId) && Users[userId].FinanceTransactions.Contains(transactionId);
|
|
||||||
|
|
||||||
public FinanceTransaction GetTransactionById(int userId, int transactionId)
|
|
||||||
{
|
|
||||||
if(!HasUser(userId)) return new FinanceTransaction();
|
|
||||||
|
|
||||||
if (UserHasTransaction(userId, transactionId)) return Users[userId].FinanceTransactions[transactionId];
|
|
||||||
|
|
||||||
return new FinanceTransaction();
|
|
||||||
}
|
|
||||||
|
|
||||||
public User AddUser(int userId, UserUpdateDto userUpdateDto)
|
|
||||||
{
|
|
||||||
if (Users.ContainsKey(userId))
|
|
||||||
{
|
|
||||||
Users[userId].Update(userUpdateDto);
|
|
||||||
return Users[userId];
|
|
||||||
}
|
|
||||||
|
|
||||||
var id = userId != -1 ? userId : Length + 1;
|
|
||||||
|
|
||||||
var newUser = new User(userUpdateDto, DateTime.Now.TimeOfDay, new FastDictionary<int, FinanceTransaction>());
|
|
||||||
|
|
||||||
Users.Add(Length, newUser);
|
|
||||||
|
|
||||||
Length++;
|
|
||||||
|
|
||||||
return newUser;
|
|
||||||
}
|
|
||||||
|
|
||||||
const string NoIdError = "No transaction was found with the specified id";
|
|
||||||
|
|
||||||
public FinanceTransaction AddFinanceTransaction(int userId, FinanceTransactionInsertDto financeTransactionDto)
|
|
||||||
{
|
|
||||||
if (Users.ContainsKey(userId))
|
|
||||||
{
|
|
||||||
var now = TimeSpan.FromTicks(DateTime.Now.Ticks);
|
|
||||||
var financeTransaction = new FinanceTransaction(financeTransactionDto, now, Users[userId].Id);
|
|
||||||
Users[userId].AddTransaction(financeTransaction);
|
|
||||||
return financeTransaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new FinanceTransaction();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public ImmutableArray<FinanceTransaction> GetUserTransactions(int userId)
|
|
||||||
{
|
|
||||||
if (Users.ContainsKey(userId))
|
|
||||||
{
|
|
||||||
return Users[userId].FinanceTransactions.Values.ToImmutableArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ImmutableArray<FinanceTransaction>();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,8 +11,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlueWest.Api", "BlueWest.Ap
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlueWest.Collections", "BlueWest.Collections\BlueWest.Collections.csproj", "{F55019A2-E2A8-4AF1-8FBC-FA99476A1B1C}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlueWest.Collections", "BlueWest.Collections\BlueWest.Collections.csproj", "{F55019A2-E2A8-4AF1-8FBC-FA99476A1B1C}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlueWest.GrpcClient", "BlueWest.GrpcClient\BlueWest.GrpcClient.csproj", "{65A6A17A-EFFB-4750-8B80-3B0690CCA59F}"
|
|
||||||
EndProject
|
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlueWest.MapTo", "include\MapTo\src\BlueWest.MapTo\BlueWest.MapTo.csproj", "{72B37540-A12F-466E-A58F-7BA2B247CB74}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlueWest.MapTo", "include\MapTo\src\BlueWest.MapTo\BlueWest.MapTo.csproj", "{72B37540-A12F-466E-A58F-7BA2B247CB74}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
|
@ -37,10 +35,6 @@ Global
|
||||||
{F55019A2-E2A8-4AF1-8FBC-FA99476A1B1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{F55019A2-E2A8-4AF1-8FBC-FA99476A1B1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{F55019A2-E2A8-4AF1-8FBC-FA99476A1B1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{F55019A2-E2A8-4AF1-8FBC-FA99476A1B1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{F55019A2-E2A8-4AF1-8FBC-FA99476A1B1C}.Release|Any CPU.Build.0 = Release|Any CPU
|
{F55019A2-E2A8-4AF1-8FBC-FA99476A1B1C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{65A6A17A-EFFB-4750-8B80-3B0690CCA59F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{65A6A17A-EFFB-4750-8B80-3B0690CCA59F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{65A6A17A-EFFB-4750-8B80-3B0690CCA59F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{65A6A17A-EFFB-4750-8B80-3B0690CCA59F}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
{72B37540-A12F-466E-A58F-7BA2B247CB74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{72B37540-A12F-466E-A58F-7BA2B247CB74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{72B37540-A12F-466E-A58F-7BA2B247CB74}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{72B37540-A12F-466E-A58F-7BA2B247CB74}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{72B37540-A12F-466E-A58F-7BA2B247CB74}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{72B37540-A12F-466E-A58F-7BA2B247CB74}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using BlueWest.Core.ComponentSystem;
|
using BlueWest.Core.ComponentSystem;
|
||||||
using BlueWest.Tools;
|
using BlueWest.Tools;
|
||||||
using PerformanceSolution.Data;
|
|
||||||
|
|
||||||
namespace PerformanceSolution.Artefacts
|
namespace PerformanceSolution.Artefacts
|
||||||
{
|
{
|
||||||
|
@ -38,22 +37,22 @@ namespace PerformanceSolution.Artefacts
|
||||||
|
|
||||||
private void GetAllUsersInConsole()
|
private void GetAllUsersInConsole()
|
||||||
{
|
{
|
||||||
var data = MemoryData.UserList;
|
/*var data = MemoryData.UserList;
|
||||||
if (data != null)
|
if (data != null)
|
||||||
{
|
{
|
||||||
Console.WriteLine(MemoryData.UserList.ToString());
|
Console.WriteLine(MemoryData.UserList.ToString());
|
||||||
|
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GetUserByIdInConsole(int id)
|
private void GetUserByIdInConsole(int id)
|
||||||
{
|
{
|
||||||
var user = MemoryData.GetUserById(id);
|
/*var user = MemoryData.GetUserById(id);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
Console.WriteLine(user.ToString());
|
Console.WriteLine(user.ToString());
|
||||||
|
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnEvent(CommandRequestEvent commandRequestEvent)
|
public void OnEvent(CommandRequestEvent commandRequestEvent)
|
||||||
|
|
|
@ -1,78 +1 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Immutable;
|
|
||||||
using BlueWest.Collections;
|
|
||||||
using BlueWest.Data;
|
|
||||||
using PerformanceSolution.Tools;
|
|
||||||
|
|
||||||
namespace PerformanceSolution.Data
|
|
||||||
{
|
|
||||||
public static class MemoryData
|
|
||||||
{
|
|
||||||
public static UserList UserList = new UserList(new FastDictionary<int, User>());
|
|
||||||
|
|
||||||
private const string SavePathName = "userData";
|
|
||||||
|
|
||||||
public static TimeSpan DateKey = new TimeSpan(2386, 0,0,0,0 );
|
|
||||||
|
|
||||||
private static bool DEBUG = true;
|
|
||||||
|
|
||||||
static MemoryData()
|
|
||||||
{
|
|
||||||
if (DEBUG)
|
|
||||||
{
|
|
||||||
UserList = GenerateMockData();
|
|
||||||
//SaveUserList();
|
|
||||||
}
|
|
||||||
|
|
||||||
//LoadUsers();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void SaveUserList(UserList userList)
|
|
||||||
{
|
|
||||||
//UserList = userList;
|
|
||||||
//SaveLoadManager.Save(UserList, SavePathName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void LoadUsers()
|
|
||||||
{
|
|
||||||
UserList = SaveLoadManager.Load<UserList>(SavePathName);
|
|
||||||
UserList ??= new UserList(new FastDictionary<int, User>());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static FinanceTransaction GetTransactionById(int userId, int transactionId) => UserList.GetTransactionById(userId, transactionId);
|
|
||||||
|
|
||||||
private static void SaveUserList() => SaveUserList(UserList);
|
|
||||||
|
|
||||||
public static User AddOrModifyUser(UserUpdateDto userUpdateDto, int userId = -1)
|
|
||||||
{
|
|
||||||
return UserList.AddUser(userId, userUpdateDto);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static UserList GenerateMockData()
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
var transactions = new FastDictionary<int, FinanceTransaction>();
|
|
||||||
|
|
||||||
var u = new User(DateTime.Now.TimeOfDay, "Benny", transactions);
|
|
||||||
var list = new FastDictionary<int, User>(10);
|
|
||||||
var financeTransaction = new FinanceTransaction(TimeSpan.FromTicks(DateTime.Now.Ticks), u.Id, "", 0.00, 0.00, 0.00,
|
|
||||||
TimeSpan.FromTicks(DateTime.Now.Ticks), TimeSpan.FromTicks(DateTime.Now.Ticks));
|
|
||||||
transactions.Add(1, financeTransaction);
|
|
||||||
|
|
||||||
list.Add(0, u);
|
|
||||||
|
|
||||||
return new UserList(list);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static User GetUserById(int userId)
|
|
||||||
{
|
|
||||||
return UserList.GetUserById(userId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ImmutableArray<FinanceTransaction> GetUserTransactions(int userId)
|
|
||||||
{
|
|
||||||
return UserList.GetUserTransactions(userId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue