Commit 1c91eb52 authored by Sandra Westerhoff's avatar Sandra Westerhoff Committed by Petar Hristov
Browse files

Update: Setting Quota values by default

parent 4981f221
......@@ -211,13 +211,14 @@ namespace Coscine.Api.Project.Controllers
Name = x.DisplayName,
Used = CalculateUsed(x, projectGuid),
Allocated = projectQuota == null ? 0 : projectQuota.Quota,
Maximum = projectQuota.MaxQuota
Maximum = projectQuota == null ? 0 : projectQuota.MaxQuota
};
}
private int GetMaxQuota(Guid projectId, Guid resourceTypeId)
{
return _projectQuotaModel.GetWhere(x => x.ProjectId == projectId && x.ResourceTypeId == resourceTypeId).MaxQuota;
var projectQuota = _projectQuotaModel.GetWhere(x => x.ProjectId == projectId && x.ResourceTypeId == resourceTypeId);
return projectQuota == null ? 0 : projectQuota.MaxQuota;
}
private int CalculateUsed(ResourceType resourceType, Guid projectId)
......@@ -284,8 +285,8 @@ namespace Coscine.Api.Project.Controllers
Id = resourceTypeGuid,
Name = resourceType.DisplayName,
Used = CalculateUsed(resourceType, projectGuid),
Allocated = projectQuota.Quota,
Maximum = projectQuota.MaxQuota
Allocated = projectQuota == null ? 0 : projectQuota.Quota,
Maximum = projectQuota == null ? 0 : projectQuota.MaxQuota
};
return Json(projectQuotaReturnObject);
......@@ -407,9 +408,24 @@ namespace Coscine.Api.Project.Controllers
return BadRequest($"Cannot set quota to {updateProjectQuotaObject.Allocated}. It would exceed the limit of {maxAvailabe}");
}
projectQuotaForCurrent.Quota = updateProjectQuotaObject.Allocated;
_projectQuotaModel.Update(projectQuotaForCurrent);
var defaultQuotas = _rdfStoreConnector.GetQuotaDefault(user.Id.ToString());
var defaultQuota = defaultQuotas.FirstOrDefault(q => q.ResourceType == resourceType.DisplayName);
if (projectQuotaForCurrent == null)
{
var projectQuota = new ProjectQuota
{
MaxQuota = defaultQuota == null ? 0 : defaultQuota.DefaultMaxQuota,
Quota = defaultQuota == null ? 0 : defaultQuota.DefaultQuota,
ProjectId = project.Id,
ResourceTypeId = resourceType.Id
};
_projectQuotaModel.Insert(projectQuota);
}
else
{
projectQuotaForCurrent.Quota = updateProjectQuotaObject.Allocated;
_projectQuotaModel.Update(projectQuotaForCurrent);
}
return NoContent();
}
......@@ -570,7 +586,20 @@ namespace Coscine.Api.Project.Controllers
return Unauthorized("User is not allowed to create SubProjects.");
}
var project = _projectModel.StoreFromObject(projectObject, user, _rdfStoreConnector.GetQuotaDefault(user.Id.ToString()));
var defaultQuotas = _rdfStoreConnector.GetQuotaDefault(user.Id.ToString());
var projectQuotas = defaultQuotas.Select(defaulQuota =>
{
var resourceType = _resourceTypeModel.GetWhere(resourceType => resourceType.DisplayName == defaulQuota.ResourceType);
return new ProjectQuota
{
MaxQuota = defaulQuota.DefaultMaxQuota,
Quota = defaulQuota.DefaultQuota,
ResourceTypeId = resourceType.Id
};
});
var project = _projectModel.StoreFromObject(projectObject, user, projectQuotas);
if (projectObject.ParentId != new Guid()
// Both an owner and a member can add subprojects to projects
......@@ -621,7 +650,7 @@ namespace Coscine.Api.Project.Controllers
Issuer = x.Issuer,
ProjectId = x.Project,
RoleId = x.Role,
UserMail = x.InviteeEmail
UserMail = x.InviteeEmail
});
return new ActionResult<IEnumerable<InvitationReturnObject>>(invitations);
......@@ -709,7 +738,7 @@ namespace Coscine.Api.Project.Controllers
{
var invitation = _invitationModel.GetById(invitationId);
if(invitation == null)
if (invitation == null)
{
return NotFound("Invitation was not found.");
}
......@@ -738,7 +767,7 @@ namespace Coscine.Api.Project.Controllers
var invitation = _invitationModel.GetByToken(token);
if(invitation == null)
if (invitation == null)
{
return NotFound("Invitation was not found.");
}
......@@ -795,7 +824,7 @@ namespace Coscine.Api.Project.Controllers
return false;
}
}
private void LogAnalyticsViewHome(List<string> projectIds)
{
_coscineLogger.AnalyticsLog(
......
......@@ -19,9 +19,9 @@
<ItemGroup>
<PackageReference Include="Coscine.Action" Version="2.*-*" />
<PackageReference Include="Coscine.ApiCommons" Version="2.*-*" />
<PackageReference Include="Coscine.Database" Version="2.*-*" />
<PackageReference Include="Coscine.Database" Version="2.6.0-issues-0028-maxq0002" />
<PackageReference Include="Coscine.Logging" Version="2.*-*" />
<PackageReference Include="Coscine.Metadata" Version="2.*-*" />
<PackageReference Include="Coscine.Metadata" Version="2.3.0-issues-0028-maxq0002" />
<PackageReference Include="Coscine.ResourceLoader" Version="2.*-*" />
<PackageReference Include="Coscine.ResourceTypeBase" Version="2.*-*" />
</ItemGroup>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment