Skip to content

Commit

Permalink
#21 - added capability to adding the space
Browse files Browse the repository at this point in the history
  • Loading branch information
sipakov committed May 23, 2023
1 parent 280dc87 commit 2376259
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 77 deletions.
6 changes: 1 addition & 5 deletions AlgoTecture.Domain/Models/SubSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ public SubSpace()

public Guid SubSpaceId { get; set; }

public int SubSpaceIdHash { get; set; }

private List<SubSpace> _subSpaces;

public List<Dictionary<string, string>> Properties { get; set; }

public double Area { get; set; }
public Dictionary<string, string> Properties { get; set; }

public int UtilizationTypeId { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,31 @@ public void ConversationClassToJsonSimpleTest()
SpaceAddress = "Unterstaldig 1 6106 Werthenstein",
Latitude = 47.04173191647986,
Longitude = 8.097301555686508,
SpaceProperty = new AddSpacePropertyModel
SpaceProperty = new SpaceProperty
{
Name = string.Empty,
Description = string.Empty,
Properties = new Dictionary<string, string>(){{"BuildingName", "Name"}, {"Area", "100"}},

SubSpaces = new List<AddSubSpaceModel>()
SubSpaces = new List<SubSpace>()
{
new AddSubSpaceModel
new SubSpace
{
UtilizationTypeId = 1,
Description = string.Empty,
Properties = new Dictionary<string, string>(){{"Levels", "2"}, {"Area", "45"}}
},
new AddSubSpaceModel
new SubSpace
{
UtilizationTypeId = 1,
Description = string.Empty,
Properties = new Dictionary<string, string>(){{"Levels", "2"}, {"Area", "44"}}
Properties = new Dictionary<string, string>(){{"Levels", "2"}, {"Area", "44"}},
Subspaces = new List<SubSpace>(){ new SubSpace
{
UtilizationTypeId = 1,
Description = string.Empty,
Properties = new Dictionary<string, string>(){{"Levels", "1"}, {"Area", "10"}}
}}
}
}
}
Expand Down
57 changes: 55 additions & 2 deletions AlgoTecture.Libraries.Spaces/Implementations/SpaceService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,66 @@
using AlgoTecture.Data.Persistence.Core.Interfaces;
using AlgoTecture.Domain.Models;
using AlgoTecture.Domain.Models.RepositoryModels;
using AlgoTecture.Libraries.Spaces.Interfaces;
using AlgoTecture.Libraries.Spaces.Models.Dto;
using Newtonsoft.Json;

namespace AlgoTecture.Libraries.Spaces.Implementations;

public class SpaceService : ISpaceService
{
public Task<Space> AddSpace(AddSpaceModel addSpaceModel)
private readonly IUnitOfWork _unitOfWork;

public SpaceService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}

public async Task<Space> AddSpace(AddSpaceModel addSpaceModel)
{
var targetSpace = await _unitOfWork.Spaces.GetByCoordinates(addSpaceModel.Latitude, addSpaceModel.Longitude);

if (targetSpace != null)
{
throw new InvalidOperationException($"Space with coordinates {addSpaceModel.Latitude},{addSpaceModel.Longitude} already exists");
}

var spaceProperty = new SpaceProperty()
{
SpacePropertyId = Guid.NewGuid(),
Name = addSpaceModel.SpaceProperty.Name,
Description = addSpaceModel.SpaceProperty.Description,
OwnerId = 0,
ContractId = 0,
Properties = addSpaceModel.SpaceProperty.Properties,
};

RecursiveFindAndAddGuidToSubSpace(addSpaceModel.SpaceProperty.SubSpaces);
spaceProperty.SubSpaces = addSpaceModel.SpaceProperty.SubSpaces;

var serializedSpaceProperty = JsonConvert.SerializeObject(spaceProperty);

var entityToInsert = new Space
{
Latitude = addSpaceModel.Latitude, Longitude = addSpaceModel.Longitude, SpaceAddress = addSpaceModel.SpaceAddress,
UtilizationTypeId = addSpaceModel.UtilizationTypeId, SpaceProperty = serializedSpaceProperty
};

var insertedEntity = await _unitOfWork.Spaces.Add(entityToInsert);
await _unitOfWork.CompleteAsync();

return insertedEntity;
}

private static void RecursiveFindAndAddGuidToSubSpace(List<SubSpace> subSpaces)
{
return null;
for (var i = 0; i < subSpaces.Count; i++)
{
if (subSpaces[i].SubSpaceId == Guid.Empty)
{
subSpaces[i].SubSpaceId = Guid.NewGuid();
}
RecursiveFindAndAddGuidToSubSpace(subSpaces[i].Subspaces);
}
}
}
4 changes: 3 additions & 1 deletion AlgoTecture.Libraries.Spaces/Models/Dto/AddSpaceModel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using AlgoTecture.Domain.Models;

namespace AlgoTecture.Libraries.Spaces.Models.Dto
{
public class AddSpaceModel
Expand All @@ -10,6 +12,6 @@ public class AddSpaceModel

public double Longitude { get; set; }

public AddSpacePropertyModel SpaceProperty { get; set; }
public SpaceProperty SpaceProperty { get; set; }
}
}
14 changes: 0 additions & 14 deletions AlgoTecture.Libraries.Spaces/Models/Dto/AddSpacePropertyModel.cs

This file was deleted.

25 changes: 0 additions & 25 deletions AlgoTecture.Libraries.Spaces/Models/Dto/AddSubSpaceModel.cs

This file was deleted.

11 changes: 5 additions & 6 deletions AlgoTecture.TelegramBot/Controllers/TelegramBotTestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ public TelegramBotTestController(IUnitOfWork unitOfWork, IGeoAdminSearcher geoAd
}

[Action]
private async Task PressGetSubSpacePropertiesButton(long spaceId, int subSpaceIdHash)
private async Task PressGetSubSpacePropertiesButton(long spaceId)
{
var targetSpace = await _unitOfWork.Spaces.GetById(spaceId);
if (targetSpace == null) return;

var targetSpaceProperty = JsonConvert.DeserializeObject<SpaceProperty>(targetSpace.SpaceProperty);
var targetSubSpace = targetSpaceProperty.SubSpaces.FirstOrDefault(x => x.SubSpaceIdHash == subSpaceIdHash);
var targetSubSpace = new SubSpace();
if (targetSubSpace == null) return;

Button("Update");
Button("Upload photo");
Button("Remove");
await Send($"{targetSpace.SpaceAddress}{Environment.NewLine}Area: {targetSubSpace.Area}{Environment.NewLine}Contract: none ");
await Send($"{targetSpace.SpaceAddress}{Environment.NewLine}Area: {Environment.NewLine}Contract: none ");
}


Expand Down Expand Up @@ -129,15 +129,14 @@ private async Task PressAddressToRentButton(string geoAdminFeatureId)
{
OwnerId = user.Id,
SubSpaceId = newSubSpaceId,
SubSpaceIdHash = newSubSpaceId.GetHashCode(),
UtilizationTypeId = 11,
}
}
};
newSpace.SpaceProperty = JsonConvert.SerializeObject(newSpaceProperty);
await _unitOfWork.CompleteAsync();
_telegramToAddressResolver.RemoveAddressListByChatId(chatId.Value);
PressGetSubSpacePropertiesButton(spaceEntity.Id, newSpaceProperty.SubSpaces.First().SubSpaceIdHash);
PressGetSubSpacePropertiesButton(spaceEntity.Id);
//await Send(targetAddress.Address);
}
else
Expand All @@ -149,7 +148,7 @@ private async Task PressAddressToRentButton(string geoAdminFeatureId)
var counter = 1;
foreach (var userSubSpace in userSubSpaces)
{
Button($"({counter})", Q(PressGetSubSpacePropertiesButton, targetSpace.Id, userSubSpace.SubSpaceIdHash));
Button($"({counter})", Q(PressGetSubSpacePropertiesButton, targetSpace.Id));
}
}

Expand Down
6 changes: 6 additions & 0 deletions AlgoTecture.WebApi/AlgoTecture.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@
<ProjectReference Include="..\AlgoTecture.Data.Persistence\AlgoTecture.Data.Persistence.csproj" />
</ItemGroup>

<ItemGroup>
<Content Update="hosting.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
36 changes: 18 additions & 18 deletions AlgoTecture.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@ public static async Task Main(string[] args)
webAppBuilder.Services.Configure<AuthenticationOptions>(webAppBuilder.Configuration.GetSection("AuthenticationOptions"));
webAppBuilder.Services.TryAddTransient<ISubSpaceService, SubSpaceService>();

var jwtIssuer = webAppBuilder.Configuration.GetSection("AuthenticationOptions").GetChildren().First(x=>x.Key == "JwtIssuer").Value;
var jwtAlgotectureSecret = webAppBuilder.Configuration.GetSection("AuthenticationOptions").GetChildren().First(x=>x.Key == "JwtAlgotectureSecret").Value;

webAppBuilder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = jwtIssuer,
ValidateAudience = true,
ValidAudience = AuthenticationConstants.Audience,
ValidateLifetime = true,
IssuerSigningKey = AuthenticationConstants.GetSymmetricSecurityKey(jwtAlgotectureSecret),
ValidateIssuerSigningKey = true
};
});
// var jwtIssuer = webAppBuilder.Configuration.GetSection("AuthenticationOptions").GetChildren().First(x=>x.Key == "JwtIssuer").Value;
// var jwtAlgotectureSecret = webAppBuilder.Configuration.GetSection("AuthenticationOptions").GetChildren().First(x=>x.Key == "JwtAlgotectureSecret").Value;
//
// webAppBuilder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
// .AddJwtBearer(options =>
// {
// options.RequireHttpsMetadata = false;
// options.TokenValidationParameters = new TokenValidationParameters
// {
// ValidateIssuer = true,
// ValidIssuer = jwtIssuer,
// ValidateAudience = true,
// ValidAudience = AuthenticationConstants.Audience,
// ValidateLifetime = true,
// IssuerSigningKey = AuthenticationConstants.GetSymmetricSecurityKey(jwtAlgotectureSecret),
// ValidateIssuerSigningKey = true
// };
// });

webAppBuilder.Services.AddControllers();

Expand Down
2 changes: 1 addition & 1 deletion AlgoTecture.WebApi/hosting.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"urls": "http://0.0.0.0:5000"
"urls": "http://0.0.0.0:5100"
}

0 comments on commit 2376259

Please sign in to comment.