Skip to content

Commit c6cfe08

Browse files
committed
Fix issue with control reference not getting added to project reference
Control references were not being added to project reference . This was happening due to incorrect impelementation of add and update methods. This is fixed now.
1 parent b0db841 commit c6cfe08

File tree

3 files changed

+66
-28
lines changed

3 files changed

+66
-28
lines changed

src/Pixel.Persistence.Respository/Interfaces/IReferencesRepository.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public interface IReferencesRepository
3131
/// <returns></returns>
3232
Task SetEditorReferences(string projectId, string projectVersion, EditorReferences editorReferences);
3333

34+
/// <summary>
35+
/// Check if any version of control is in use by any version of any project
36+
/// </summary>
37+
/// <param name="controlId"></param>
38+
/// <returns></returns>
39+
Task<bool> IsControlInUse(string controlId);
3440

3541
/// <summary>
3642
/// Check if a given version of project has an existing reference to specified control
@@ -74,15 +80,24 @@ public interface IReferencesRepository
7480
/// <param name="projectVersion"></param>
7581
/// <param name="controlReference"></param>
7682
/// <returns></returns>
77-
//Task<bool> HasPrefabReference(string projectId, string projectVersion, PrefabReference prefabReference);
83+
Task<bool> HasPrefabReference(string projectId, string projectVersion, PrefabReference prefabReference);
84+
85+
/// <summary>
86+
/// Add PrefabReference for a given version of project
87+
/// </summary>
88+
/// <param name="projectId"></param>
89+
/// <param name="projectVersion"></param>
90+
/// <param name="prefabReference"></param>
91+
/// <returns></returns>
92+
Task AddPrefabReference(string projectId, string projectVersion, PrefabReference prefabReference);
7893

7994
/// <summary>
80-
/// Add or update PrefabReferences for a given version of project
95+
/// Update an existing PrefabReference for a given version of project
8196
/// </summary>
8297
/// <param name="projectId"></param>
8398
/// <param name="projectVersion"></param>
8499
/// <param name="prefabReference"></param>
85100
/// <returns></returns>
86-
Task AddOrUpdatePrefabReference(string projectId, string projectVersion, PrefabReference prefabReference);
101+
Task UpdatePrefabReference(string projectId, string projectVersion, PrefabReference prefabReference);
87102

88103
}

src/Pixel.Persistence.Respository/ReferencesRepository.cs

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public async Task AddProjectReferences(string projectId, string projectVersion,
4747
projectReferences.ProjectId = projectId;
4848
projectReferences.ProjectVersion = projectVersion;
4949
await this.referencesCollection.InsertOneAsync(projectReferences);
50+
logger.LogInformation("ProjectReferences was added to version : '{0}' of project : '{1}'", projectVersion, projectId);
51+
}
52+
53+
/// <inheritdoc/>
54+
public async Task<bool> IsControlInUse(string controlId)
55+
{
56+
var filter = Builders<ProjectReferences>.Filter.ElemMatch(x => x.ControlReferences,
57+
Builders<ControlReference>.Filter.Eq(x => x.ControlId, controlId));
58+
long count = await this.referencesCollection.CountDocumentsAsync(filter);
59+
return count > 0;
5060
}
5161

5262
/// <inheritdoc/>
@@ -64,20 +74,21 @@ public async Task<bool> HasControlReference(string projectId, string projectVers
6474
/// <inheritdoc/>
6575
public async Task AddControlReference(string projectId, string projectVersion, ControlReference controlReference)
6676
{
67-
var filter = Builders<ProjectReferences>.Filter.Eq(x => x.ProjectId, projectId) & Builders<ProjectReferences>.Filter.Eq(x => x.ProjectVersion, projectVersion)
68-
& Builders<ProjectReferences>.Filter.ElemMatch(x => x.ControlReferences, Builders<ControlReference>.Filter.Eq(x => x.ControlId, controlReference.ControlId));
69-
var update = Builders<ProjectReferences>.Update.Set(x => x.ControlReferences[-1].Version, controlReference.Version);
70-
await this.referencesCollection.UpdateOneAsync(filter, update);
71-
logger.LogInformation("Control reference {0} was updated for project : {1}", controlReference, projectId);
77+
var filter = Builders<ProjectReferences>.Filter.Eq(x => x.ProjectId, projectId) & Builders<ProjectReferences>.Filter.Eq(x => x.ProjectVersion, projectVersion);
78+
var push = Builders<ProjectReferences>.Update.Push(t => t.ControlReferences, controlReference);
79+
await this.referencesCollection.UpdateOneAsync(filter, push);
80+
logger.LogInformation("Control reference {@0} was added to version : '{1}' of project : {2}", controlReference, projectVersion, projectId);
7281
}
7382

7483
/// <inheritdoc/>
7584
public async Task UpdateControlReference(string projectId, string projectVersion, ControlReference controlReference)
7685
{
77-
var filter = Builders<ProjectReferences>.Filter.Eq(x => x.ProjectId, projectId);
78-
var push = Builders<ProjectReferences>.Update.Push(t => t.ControlReferences, controlReference);
79-
await this.referencesCollection.UpdateOneAsync(filter, push);
80-
logger.LogInformation("Control reference {0} was added to project : {1}", controlReference, projectId);
86+
var filter = Builders<ProjectReferences>.Filter.Eq(x => x.ProjectId, projectId) & Builders<ProjectReferences>.Filter.Eq(x => x.ProjectVersion, projectVersion)
87+
& Builders<ProjectReferences>.Filter.ElemMatch(x => x.ControlReferences, Builders<ControlReference>.Filter.Eq(x => x.ControlId, controlReference.ControlId));
88+
var update = Builders<ProjectReferences>.Update.Set(x => x.ControlReferences[-1].Version, controlReference.Version);
89+
await this.referencesCollection.UpdateOneAsync(filter, update);
90+
logger.LogInformation("Control reference {@0} was updated for version : '{1}' of project : {2}", controlReference, projectVersion, projectId);
91+
8192
}
8293

8394
/// <inheritdoc/>
@@ -90,28 +101,34 @@ public async Task<bool> IsPrefabInUse(string prefabId)
90101
}
91102

92103
/// <inheritdoc/>
93-
public async Task AddOrUpdatePrefabReference(string projectId, string projectVersion, PrefabReference prefabReference)
104+
public async Task<bool> HasPrefabReference(string projectId, string projectVersion, PrefabReference prefabReference)
94105
{
95106
var filter = Builders<ProjectReferences>.Filter.Eq(x => x.ProjectId, projectId) & Builders<ProjectReferences>.Filter.Eq(x => x.ProjectVersion, projectVersion)
96-
& Builders<ProjectReferences>.Filter.ElemMatch(x => x.PrefabReferences, Builders<PrefabReference>.Filter.Eq(x => x.PrefabId, prefabReference.PrefabId));
107+
& Builders<ProjectReferences>.Filter.ElemMatch(x => x.PrefabReferences, Builders<PrefabReference>.Filter.Eq(x => x.PrefabId, prefabReference.PrefabId));
97108
var prefabReferences = (await this.referencesCollection.FindAsync<List<PrefabReference>>(filter, new FindOptions<ProjectReferences, List<PrefabReference>>()
98109
{
99110
Projection = Builders<ProjectReferences>.Projection.Expression(u => u.PrefabReferences)
100111
})).ToList().FirstOrDefault();
112+
return prefabReferences?.Contains(prefabReference) ?? false;
113+
}
101114

102-
if (prefabReferences?.Contains(prefabReference) ?? false)
103-
{
104-
var update = Builders<ProjectReferences>.Update.Set(x => x.PrefabReferences[-1].Version, prefabReference.Version);
105-
await this.referencesCollection.UpdateOneAsync(filter, update);
106-
logger.LogInformation("Prefab reference {0} was updated for project : {1}", prefabReference, projectId);
107-
}
108-
else
109-
{
110-
filter = Builders<ProjectReferences>.Filter.Eq(x => x.ProjectId, projectId) & Builders<ProjectReferences>.Filter.Eq(x => x.ProjectVersion, projectVersion);
111-
var push = Builders<ProjectReferences>.Update.Push(t => t.PrefabReferences, prefabReference);
112-
await this.referencesCollection.UpdateOneAsync(filter, push);
113-
logger.LogInformation("Prefab reference {0} was added to project : {1}", prefabReference, projectId);
114-
}
115+
/// <inheritdoc/>
116+
public async Task AddPrefabReference(string projectId, string projectVersion, PrefabReference prefabReference)
117+
{
118+
var filter = Builders<ProjectReferences>.Filter.Eq(x => x.ProjectId, projectId) & Builders<ProjectReferences>.Filter.Eq(x => x.ProjectVersion, projectVersion);
119+
var push = Builders<ProjectReferences>.Update.Push(t => t.PrefabReferences, prefabReference);
120+
await this.referencesCollection.UpdateOneAsync(filter, push);
121+
logger.LogInformation("Prefab reference {@0} was added to version : '{1}' of project : {2}", prefabReference, projectVersion, projectId);
122+
}
123+
124+
/// <inheritdoc/>
125+
public async Task UpdatePrefabReference(string projectId, string projectVersion, PrefabReference prefabReference)
126+
{
127+
var filter = Builders<ProjectReferences>.Filter.Eq(x => x.ProjectId, projectId) & Builders<ProjectReferences>.Filter.Eq(x => x.ProjectVersion, projectVersion)
128+
& Builders<ProjectReferences>.Filter.ElemMatch(x => x.PrefabReferences, Builders<PrefabReference>.Filter.Eq(x => x.PrefabId, prefabReference.PrefabId));
129+
var update = Builders<ProjectReferences>.Update.Set(x => x.PrefabReferences[-1].Version, prefabReference.Version);
130+
await this.referencesCollection.UpdateOneAsync(filter, update);
131+
logger.LogInformation("Prefab reference {@0} was updated for version : '{1}' of project : {2}", prefabReference, projectVersion, projectId);
115132
}
116133

117134
/// <inheritdoc/>

src/Pixel.Persistence.Services.Api/Controllers/ReferencesController.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,18 @@ public async Task<IActionResult> AddOrUpdateControlReference(string projectId, s
6868
[HttpPost("prefabs/{projectId}/{projectVersion}")]
6969
public async Task<IActionResult> AddOrUpdatePrefabReference(string projectId, string projectVersion, [FromBody] PrefabReference prefabReference)
7070
{
71+
var hasExistingReference = await this.referencesRepository.HasPrefabReference(projectId, projectVersion, prefabReference);
72+
if (hasExistingReference)
73+
{
74+
await this.referencesRepository.UpdatePrefabReference(projectId, projectVersion, prefabReference);
75+
return Ok();
76+
}
7177
if (await this.prefabsRepository.IsPrefabDeleted(prefabReference.PrefabId))
7278
{
7379
logger.LogWarning("Can't add prefab reference to project : {0}, version : {1}. Prefab {@2} is marked deleted.", projectId, projectVersion, prefabReference);
7480
return Conflict("Can't add prefab reference. Prefab is marked deleted.");
7581
}
76-
await this.referencesRepository.AddOrUpdatePrefabReference(projectId, projectVersion, prefabReference);
82+
await this.referencesRepository.AddPrefabReference(projectId, projectVersion, prefabReference);
7783
return Ok();
7884
}
7985

0 commit comments

Comments
 (0)