public static async Task <ProjectData> CreateProject(HttpClient project, ProjectCreationData user, bool isLocal = false) { string url; if (isLocal) { url = "/api/Projects"; } else { url = "https://projects-service.api.converge-app.net/api/projects"; } var response = await project.PostAsJsonAsync(url, user); if (response.IsSuccessStatusCode) { return(await response.Content.ReadAsAsync <ProjectData>()); } else { throw new Exception("Was unsuccessful"); } }
public async Task <IActionResult> CreateProject([FromBody] ProjectCreationData data) { var db = Database; var currentDate = DateTime.UtcNow; var currentUser = await(from u in db.Query <User>() where u.Key == HttpContext.User.Identity.Name select u).FirstOrDefaultAsync(); if (currentUser == null) { return(Unauthorized()); } var groupGraph = db.Graph("GroupUsersGraph"); // obtenemos los usuarios var usersToAdd = from u in db.Query <User>() from ud in data.Members where u.Username == ud select u; if (data.Members.Count != usersToAdd.Count()) { return(BadRequest(new { message = "Some users are invalid" })); } // En caso de que el usuario haya puesto su nombre de usuario, lo eliminamos, el se añade aparte puesto que será administrador usersToAdd = from u in usersToAdd where u.Key != currentUser.Key select u; var projectGroup = new Group { CreationDate = currentDate, Name = data.Name, GroupOwner = HttpContext.User.Identity.Name, IsRoot = true, Description = $"# {data.Name}\nEsta descripción es generada automaticamente, si eres el administrador puedes cambiarla en el panel de administrador", KanbanBoards = new List <KanbanBoard> { new KanbanBoard(data.Name) }, Events = new List <CalendarEvent>(), PreviousProject = data.PreviousProject }; // Default kanban creation projectGroup.KanbanBoards[0].Members = await usersToAdd.Select(u => new Models.Kanban.KanbanGroupMember { UserId = u.Key }).ToListAsync(); projectGroup.KanbanBoards[0].Members.Add(new Models.Kanban.KanbanGroupMember { UserId = currentUser.Key, MemberPermissions = Models.Kanban.KanbanMemberPermissions.Admin }); var createdGroup = await db.InsertAsync <Group>(projectGroup); var admin = new UsersInGroup { IsAdmin = true, JoinDate = currentDate, Group = createdGroup.Id, User = "******" + currentUser.Key, AddedBy = currentUser.Key }; // create a relation between root group and the creator await groupGraph.InsertEdgeAsync <UsersInGroup>(admin); foreach (var user in usersToAdd) { var userToAdd = new UsersInGroup { IsAdmin = false, JoinDate = currentDate, Group = createdGroup.Id, User = "******" + user.Key, AddedBy = currentUser.Key }; await groupGraph.InsertEdgeAsync <UsersInGroup>(userToAdd); var notificationMessage = string.Format($"{currentUser.Username} te ha agregado " + $"al proyecto '{projectGroup.Name}'", projectGroup.Name); var notification = new Notification { Read = false, Message = notificationMessage, Priority = NotificationPriority.Medium, Context = $"project/{createdGroup.Key}" }; // enviar la notificación await NotificationHub.Clients .Group("Users/" + user.Key) .SendAsync("notificationReceived", notification); user.Notifications.Add(notification); await db.UpdateByIdAsync <User>(user.Id, user); } return(Created("/api/projects/" + createdGroup.Key, null)); }