Skip to content
Snippets Groups Projects

Release: Sprint/2022 17 :robot:

Merged Petar Hristov requested to merge dev into master
2 files
+ 97
6
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -2,7 +2,7 @@ using Coscine.Configuration;
using Coscine.Database.DataModel;
using Coscine.Database.ReturnObjects;
using Coscine.Database.Util;
using LinqKit;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
@@ -13,6 +13,79 @@ namespace Coscine.Database.Models
{
public class ResourceModel : DatabaseModel<Resource>
{
public override Resource GetById(Guid id)
{
var expression = GetIdFromObject();
return DatabaseConnection.ConnectToDatabase((db) =>
{
return
(from tableEntry in GetITableFromDatabase(db).AsExpandable()
where expression.Invoke(tableEntry) == id
&& !tableEntry.Deleted
select tableEntry).FirstOrDefault();
});
}
public Resource GetByIdIncludingDeleted(Guid id)
{
var expression = GetIdFromObject();
return DatabaseConnection.ConnectToDatabase((db) =>
{
return
(from tableEntry in GetITableFromDatabase(db).AsExpandable()
where expression.Invoke(tableEntry) == id
select tableEntry).FirstOrDefault();
});
}
public override Resource GetWhere(Expression<Func<Resource, bool>> whereClause)
{
return DatabaseConnection.ConnectToDatabase((db) =>
{
return
(from tableEntry in GetITableFromDatabase(db).AsExpandable()
where whereClause.Invoke(tableEntry)
&& !tableEntry.Deleted
select tableEntry).FirstOrDefault();
});
}
public override IEnumerable<Resource> GetAll()
{
return DatabaseConnection.ConnectToDatabase((db) =>
{
return
(from tableEntry in GetITableFromDatabase(db)
where !tableEntry.Deleted
select tableEntry).ToList();
});
}
public override IEnumerable<Resource> GetAllWhere(Expression<Func<Resource, bool>> whereClause)
{
return DatabaseConnection.ConnectToDatabase((db) =>
{
return
(from tableEntry in GetITableFromDatabase(db).AsExpandable()
where whereClause.Invoke(tableEntry)
&& !tableEntry.Deleted
select tableEntry).ToList();
});
}
public override int Update(Resource databaseObject)
{
if (!databaseObject.Deleted)
{
return DatabaseConnection.ConnectToDatabase((db) =>
{
return (int)db.Update(databaseObject).State;
});
}
else
{
return 0;
}
}
public Resource StoreFromObject(ResourceObject resourceObject)
{
if (!resourceObject.Disciplines.Any() || resourceObject.ResourceTypeOption == null)
@@ -46,15 +119,33 @@ namespace Coscine.Database.Models
}
catch (Exception)
{
// Makes sure to delete all FK references, otherwise a delete is not possible
DeleteResource(resource);
// Makes sure to delete all references, otherwise a delete is not possible
HardDeleteResource(resource);
throw;
}
return resource;
}
public int DeleteResource(Resource resource)
public int DeleteResource(Resource databaseObject)
{
databaseObject.Deleted = true;
return DatabaseConnection.ConnectToDatabase((db) => (int)db.Update(databaseObject).State);
}
public bool IsDeleted(Guid id)
{
return DatabaseConnection.ConnectToDatabase((db) =>
{
return
(from tableEntry in GetITableFromDatabase(db)
where tableEntry.Id == id
&& tableEntry.Deleted
select tableEntry).Count() == 1;
});
}
public int HardDeleteResource(Resource resource)
{
ProjectResourceModel projectResourceModel = new ProjectResourceModel();
foreach (var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ResourceId == resource.Id))
@@ -98,7 +189,7 @@ namespace Coscine.Database.Models
join rt in db.ResourceTypes on r.TypeId equals rt.Id into joinedRt
from jrt in joinedRt.DefaultIfEmpty()
where !jp.Deleted &&
where !jp.Deleted && !r.Deleted &&
jrt.Type == "rds"
group r by jpi.OrganizationUrl into g
select new OrganizationResourceListObject(g.Key, g.ToList())).ToList();
Loading