ResourceModel.cs 2.11 KB
Newer Older
1 2
using Coscine.Api.Project.ReturnObjects;
using Coscine.Database.Model;
3 4 5
using LinqToDB;
using System;
using System.Linq;
6
using System.Linq.Expressions;
7 8 9 10 11

namespace Coscine.Api.Project.Models
{
    public class ResourceModel : DatabaseModel<Resource>
    {
12
        public ResourceModel() : base(Program.Configuration)
13 14 15 16
        {

        }

17 18 19 20 21
        public Resource StoreFromObject(ResourceObject resourceObject)
        {
            Resource resource = new Resource()
            {
                ExternalId = resourceObject.ExternalId,
22 23
                Url = resourceObject.Url,
                TypeId = resourceObject.Type.Id
24 25 26 27 28 29
            };
            resource.Type = new ResourceTypeModel().GetById(resourceObject.Type.Id);
            Insert(resource);
            return resource;
        }

30 31 32 33 34 35 36
        public bool OwnsResource(User user, Resource resource)
        {
            return DatabaseConnection.ConnectToDatabase((db) =>
            {
                return (from relation in db.ProjectRoles
                        where relation.User == user
                            && relation.Role.DisplayName == "Owner"
37 38
                            && (relation.Project.ProjectResourceProjectIdIds != null && relation.Project.ProjectResourceProjectIdIds.
                                Any((projectResource) => projectResource.Resource == resource))
39 40 41 42
                        select relation).Any();
            });
        }

43
        public override Expression<Func<Resource, Guid>> GetIdFromObject()
44
        {
45
            return databaseObject => databaseObject.Id;
46 47 48 49 50 51
        }

        public override ITable<Resource> GetITableFromDatabase(CoscineDB db)
        {
            return db.Resources;
        }
52 53 54 55 56

        public override void SetObjectId(Resource databaseObject, Guid Id)
        {
            databaseObject.Id = Id;
        }
57 58 59 60 61 62 63 64 65

        public int UpdateByObject(Resource resource, ResourceObject resourceObject)
        {
            resource.ExternalId = resourceObject.ExternalId;
            resource.TypeId = resourceObject.Type.Id;
            resource.Url = resourceObject.Url;

            return Update(resource);
        }
66 67
    }
}