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

namespace Coscine.Api.Project.Models
{
    public class ProjectModel : DatabaseModel<Coscine.Database.Model.Project>
    {

17
        public ProjectModel() : base(Program.Configuration) 
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
        {
        }

        public Coscine.Database.Model.Project StoreFromObject(ProjectObject projectObject, User user)
        {
            Coscine.Database.Model.Project project = new Coscine.Database.Model.Project()
            {
                Description = projectObject.Description,
                DisplayName = projectObject.DisplayName,
                Organization = projectObject.Organization
            };
            Insert(project);
            SetOwner(project, user);
            return project;
        }

Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
34
        public ProjectRole SetOwner(Coscine.Database.Model.Project project, User user)
35
        {
36
37
38
            ProjectRoleModel projectRoleModel = new ProjectRoleModel();

            ProjectRole projectRole = new ProjectRole()
39
            {
40
41
42
43
44
45
                RelationId = Guid.NewGuid(),
                ProjectId = project.Id,
                UserId = user.Id,
                RoleId = new RoleModel().GetWhere((x) => x.DisplayName == "Owner").Id
            };
            projectRoleModel.Insert(projectRole);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
46
47

            return projectRole;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        }

        public bool OwnsProject(User user, Coscine.Database.Model.Project project)
        {
            return DatabaseConnection.ConnectToDatabase((db) =>
            { 
                return (from relation in db.ProjectRoles
                         where relation.Project == project
                             && relation.User == user
                             && relation.Role.DisplayName == "Owner"
                             select relation).Any();            
            });
        }

        public void AddResource(Coscine.Database.Model.Project project, Resource resource)
        {
            if(project.ProjectResourceProjectIdIds.Any((pr) => pr.Resource == resource))
            {
                throw new InvalidOperationException("Resource is already assigned to project!");
            }
            ProjectResource projectResource = new ProjectResource();
            projectResource.Project = project;
            projectResource.Resource = resource;
            project.ProjectResourceProjectIdIds.Append(projectResource);
            Update(project);
        }

75
        public override Expression<Func<Database.Model.Project, Guid>> GetIdFromObject()
76
        {
77
            return databaseObject => databaseObject.Id;
78
79
80
81
82
83
        }

        public override ITable<Database.Model.Project> GetITableFromDatabase(CoscineDB db)
        {
            return db.Projects;
        }
84
85
86
87
88

        public override void SetObjectId(Database.Model.Project databaseObject, Guid Id)
        {
            databaseObject.Id = Id;
        }
89
90
    }
}