MetadataModel.cs 2.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons.Models;
using Coscine.Database.Model;
using LinqToDB;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Coscine.Api.Project.Models
{
    public class MetadataModel : DatabaseModel<Resource>
    {

        public MetadataModel() : base(Program.Configuration)
        {
        }

        public override Expression<Func<Resource, Guid>> GetIdFromObject()
        {
            throw new NotImplementedException();
        }

        public override void SetObjectId(Resource databaseObject, Guid id)
        {
            throw new NotImplementedException();
        }

        public override ITable<Resource> GetITableFromDatabase(CoscineDB db)
        {
            throw new NotImplementedException();
        }

        public Resource StoreFromObject(ResourceObject resourceObject)
        {
            Resource resource = new Resource()
            {
                DisplayName = resourceObject.DisplayName,
                ResourceName = resourceObject.ResourceName,
                Keywords = resourceObject.Keywords,
                UsageRights = resourceObject.UsageRights,
                TypeId = resourceObject.Type.Id,
                Type = new ResourceTypeModel().GetById(resourceObject.Type.Id),
                VisibilityId = resourceObject.Visibility.Id,
                LicenseId = resourceObject.License.Id
            };


            return resource;
        }

        public string GenerateId(string resourceId, string filename, string version)
        {
            return $"https://purl.org/coscine/md/{resourceId}/{filename}/{version}/";
        }

        public bool IsProjectMember(User user, Resource resource)
        {
            return DatabaseConnection.ConnectToDatabase((db) =>
            {
                return (from relation in db.ProjectRoles
61
                        where relation.UserId == user.Id
62
63
64
65
66
67
                            && (relation.Role.DisplayName == "Owner" || relation.Role.DisplayName == "Member")
                            && (relation.Project.ProjectResourceProjectIdIds != null && relation.Project.ProjectResourceProjectIdIds.
                                Any((projectResource) => projectResource.Resource == resource))
                        select relation).Any();
            });
        }
68
69
70
71
72
73
74
75
76
77
78
79

        public bool IsProjectMember(User user, Guid projectId)
        {
            return DatabaseConnection.ConnectToDatabase((db) =>
            {
                return (from relation in db.ProjectRoles
                        where relation.UserId == user.Id
                            && (relation.Role.DisplayName == "Owner" || relation.Role.DisplayName == "Member")
                            && (relation.ProjectId == projectId)
                        select relation).Any();
            });
        }
80
81
    }
}