| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using MUNity.Database.Models.Conference; |
| | 3 | | using MUNity.Schema.Project; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Text; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | |
|
| | 10 | | namespace MUNity.Services |
| | 11 | | { |
| | 12 | | public class ProjectService |
| | 13 | | { |
| | 14 | | private readonly MUNity.Database.Context.MunityContext context; |
| | 15 | |
|
| | 16 | | public CreateProjectResponse CreateProject(CreateProjectModel model) |
| 0 | 17 | | { |
| 0 | 18 | | var response = new CreateProjectResponse(); |
| | 19 | |
|
| | 20 | | // TODO: Check user is allowed to create projects! |
| | 21 | |
|
| 0 | 22 | | var organization = context.Organizations.FirstOrDefault(n => n.OrganizationId == model.OrganizationId); |
| 0 | 23 | | if (organization == null) |
| 0 | 24 | | { |
| 0 | 25 | | response.Status = CreateProjectResponse.CreateProjectStatus.OrganizationNotFound; |
| 0 | 26 | | return response; |
| | 27 | | } |
| | 28 | |
|
| | 29 | |
|
| 0 | 30 | | var project = new Project() |
| 0 | 31 | | { |
| 0 | 32 | | ProjectName = model.Name, |
| 0 | 33 | | ProjectShort = model.Short, |
| 0 | 34 | | ProjectOrganization = organization |
| 0 | 35 | | }; |
| | 36 | |
|
| 0 | 37 | | string cleanedId = Util.IdGenerator.AsPrimaryKey(model.Short); |
| 0 | 38 | | if (context.Projects.All(n => n.ProjectId != cleanedId)) |
| 0 | 39 | | project.ProjectId = cleanedId; |
| | 40 | |
|
| 0 | 41 | | context.Projects.Add(project); |
| 0 | 42 | | context.SaveChanges(); |
| 0 | 43 | | response.ProjectId = project.ProjectId; |
| 0 | 44 | | return response; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public ProjectDashboardInfo GetDashboardInfo(string projectId) |
| 0 | 48 | | { |
| 0 | 49 | | return context.Projects.AsNoTracking() |
| 0 | 50 | | .Select(n => new ProjectDashboardInfo() |
| 0 | 51 | | { |
| 0 | 52 | | Name = n.ProjectName, |
| 0 | 53 | | Short = n.ProjectShort, |
| 0 | 54 | | ProjectId = n.ProjectId, |
| 0 | 55 | | Conferences = n.Conferences.Select(a => new ProjectDashboardConferenceInfo() |
| 0 | 56 | | { |
| 0 | 57 | | ConferenceId = a.ConferenceId, |
| 0 | 58 | | EndDate = a.EndDate, |
| 0 | 59 | | Name = a.Name, |
| 0 | 60 | | FullName = a.FullName, |
| 0 | 61 | | Short = a.ConferenceShort, |
| 0 | 62 | | StartDate = a.StartDate, |
| 0 | 63 | | CreationDate = a.CreationDate, |
| 0 | 64 | | CreationUserUsername = a.CreationUser.UserName |
| 0 | 65 | | }).ToList() |
| 0 | 66 | | }) |
| 0 | 67 | | .FirstOrDefault(n => n.ProjectId == projectId); |
| 0 | 68 | | } |
| | 69 | |
|
| 0 | 70 | | public ProjectService(Database.Context.MunityContext context) |
| 0 | 71 | | { |
| 0 | 72 | | this.context = context; |
| 0 | 73 | | } |
| | 74 | | } |
| | 75 | | } |