ResourceModel.cs 1.62 KB
Newer Older
1 2
using Coscine.Api.Project.ReturnObjects;
using Coscine.Database.Model;
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
using LinqToDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Coscine.Api.Project.Models
{
    public class ResourceModel : DatabaseModel<Resource>
    {
        public ResourceModel() : base(Program.Configurator.Configuration)
        {

        }

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

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        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"
                            && relation.Project.ProjectResourceProjectIdIds.
                                Any((projectResource) => projectResource.Resource == resource)
                        select relation).Any();
            });
        }

        public override Guid GetIdFromObject(Resource databaseObject)
        {
            return databaseObject.Id;
        }

        public override ITable<Resource> GetITableFromDatabase(CoscineDB db)
        {
            return db.Resources;
        }
    }
}