|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @ngdoc function |
| 5 | + * @name requirementsBazaarWebFrontendApp.controller:CreateCtrl |
| 6 | + * @description |
| 7 | + * # CreateCtrl |
| 8 | + * Controller of the requirementsBazaarWebFrontendApp |
| 9 | + */ |
| 10 | +angular.module('requirementsBazaarWebFrontendApp') |
| 11 | + .controller('CreateCtrl', function ($scope, reqBazService, UtilityService, $upload) { |
| 12 | + |
| 13 | + $scope.showCreateDiv = false; |
| 14 | + $scope.newName = ''; |
| 15 | + $scope.newDesc = ''; |
| 16 | + |
| 17 | + $scope.createAttachments = []; |
| 18 | + |
| 19 | + //Creating a requirement |
| 20 | + $scope.submitReq = function(){ |
| 21 | + if(!UtilityService.isEmpty($scope.newName,'Choose requirement name') && !UtilityService.isEmpty($scope.newDesc,'Choose requirement description')) { |
| 22 | + if($scope.createAttachments.length !== 0){ |
| 23 | + //TODO save the attachments |
| 24 | + $scope.createAttachments = []; |
| 25 | + UtilityService.showFeedback('Warning: Attachments were not included !'); |
| 26 | + } |
| 27 | + console.log('submit requirement'); |
| 28 | + var requirement = {title: $scope.newName, description: $scope.newDesc, projectId: $scope.activeProject.id, leadDeveloperId: 1, creatorId: 1}; |
| 29 | + reqBazService.createRequirement($scope.activeProject.id, $scope.activeComponent.id, requirement) |
| 30 | + .success(function (message) { |
| 31 | + console.log(message); |
| 32 | + if (message.hasOwnProperty('errorCode')) { |
| 33 | + UtilityService.showFeedback('Warning: Requirement was not created !'); |
| 34 | + } else { |
| 35 | + //Add missing values to the newly created requirement |
| 36 | + requirement.id = message.id; |
| 37 | + requirement.creator = {firstName: 'loading'}; |
| 38 | + requirement.leadDeveloper = {firstName: 'loading'}; |
| 39 | + requirement.followers = []; |
| 40 | + requirement.developers = []; |
| 41 | + requirement.contributors = []; |
| 42 | + requirement.attachments = []; |
| 43 | + requirement.components = []; |
| 44 | + |
| 45 | + //Add the requirement to the first position |
| 46 | + $scope.requirements.splice(0, 0, requirement); |
| 47 | + $scope.clearSubmit(); |
| 48 | + } |
| 49 | + }) |
| 50 | + .error(function (error) { |
| 51 | + //This error only catches unknown server errors, usual errorCodes are sent with success message |
| 52 | + console.log(error); |
| 53 | + UtilityService.showFeedback('Warning: Requirement was not created !'); |
| 54 | + }); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + |
| 59 | + $scope.submitProject = function(){ |
| 60 | + if(!UtilityService.isEmpty($scope.newName,'Choose project name') && !UtilityService.isEmpty($scope.newDesc, 'Choose project description')){ |
| 61 | + console.log('submit new project'); |
| 62 | + var project = {description: $scope.newDesc, name: $scope.newName, visibility: 'PUBLIC', leaderId: 1}; |
| 63 | + reqBazService.createProject(project) |
| 64 | + .success(function (message) { |
| 65 | + console.log(message); |
| 66 | + if(message.hasOwnProperty('errorCode')){ |
| 67 | + UtilityService.showFeedback('Warning: Project was not created !'); |
| 68 | + }else { |
| 69 | + UtilityService.showFeedback('Project was created'); |
| 70 | + |
| 71 | + //The project is added to be the first element and will be active |
| 72 | + project.id = message.id; |
| 73 | + $scope.activeProject = project; |
| 74 | + $scope.projects.splice(0, 0, $scope.activeProject); |
| 75 | + $scope.selectProj($scope.activeProject); |
| 76 | + $scope.clearSubmit(); |
| 77 | + } |
| 78 | + }) |
| 79 | + .error(function (error) { |
| 80 | + console.log(error); |
| 81 | + UtilityService.showFeedback('Warning: Project was not created !'); |
| 82 | + }); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + $scope.clearSubmit = function(){ |
| 87 | + $scope.newName = ''; |
| 88 | + $scope.newDesc = ''; |
| 89 | + $scope.showCreateDiv = false; |
| 90 | + }; |
| 91 | + |
| 92 | + $scope.newAttachments = function(files){ |
| 93 | + if(files[0].type.indexOf('image/') > -1){ |
| 94 | + $scope.createAttachments.push({'file':files[0],'URL':URL.createObjectURL(files[0])}); |
| 95 | + } |
| 96 | + if(files[0].type.indexOf('application/pdf') > -1 || files[0].type.indexOf('video/') > -1){ |
| 97 | + console.log('is pdf or video'); |
| 98 | + $scope.createAttachments.push({'file':files[0],'URL':null}); |
| 99 | + } |
| 100 | + console.log($scope.createAttachments); |
| 101 | + }; |
| 102 | + }); |
0 commit comments