Advertisement
Guest User

Untitled

a guest
May 26th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const logger = require('../config/appconfig').logger;
  2. const database = require('../datalayer/mssql.dao');
  3. const assert = require('assert')
  4. const session = require
  5. let functions = {};
  6.  
  7.  
  8.  
  9. functions.getAllPrograms = (req, res, next) => {
  10.  
  11.     logger.info('GET /api/programs called');
  12.  
  13.     const query = 'SELECT * FROM Program';
  14.  
  15.     database.executeQuery(query, (err, rows) => {
  16.  
  17.         if (err) {
  18.             const errorObject = {
  19.                 message: 'Something went wrong in the database.',
  20.                 code: 500
  21.             };
  22.             return next(errorObject);
  23.         }
  24.  
  25.         if (rows) {
  26.             let result = {};
  27.  
  28.             if (rows.recordset.length === 0) {
  29.                 // TODO: Change this to an error code?
  30.                 res.status(200).json({ result: {} });
  31.             }
  32.  
  33.             rows
  34.                 .recordsets[0].forEach(function (value, index, array) {
  35.  
  36.                     const id = value.ProgramID;
  37.  
  38.                     if (!result.hasOwnProperty(id)) {
  39.  
  40.                         result[id] = {
  41.  
  42.                             ProgramID: id,
  43.                             Title: value.Title,
  44.                             Description: value.Description,
  45.                             Author: value.Author,
  46.                             ISBN: value.ISBN,
  47.                             Publisher: value.Publisher,
  48.                             YearOfPublication: value.YearOfPublication,
  49.                             PlaceOfPublication: value.PlaceOfPublication,
  50.                             AddressPublisher: value.AddressPublisher,
  51.                             PhoneNumberOfPublisher: value.PhoneNumberOfPublisher,
  52.                             CostOfProgram: value.CostOfProgram,
  53.                             WebsitePublisher: value.WebsitePublisher,
  54.                             AgeRange: value.AgeRange,
  55.                             InclusionCriteria: value.InclusionCriteria,
  56.                             ExclusionCriteria: value.ExclusionCriteria,
  57.                             AlternativeTargetGroups: value.AlternativeTargetGroups,
  58.                             ScientificBasis: value.ScientificBasis,
  59.                             UnderlyingTheory: value.UnderlyingTheory,
  60.                             EmpiricallyTested: value.EmpiricallyTested,
  61.                             Efficiency: value.Efficiency,
  62.                             TrainingProgram: value.TrainingProgram,
  63.                             ViewOnImpairement: value.ViewOnImpairement,
  64.                             ViewOnCare: value.ViewOnCare,
  65.                             ViewOnQualityOfCare: value.ViewOnQualityOfCare,
  66.                             PotrayalOfMankind: value.PotrayalOfMankind,
  67.                             BuiltInAssessment: value.BuiltInAssessment,
  68.                             ExternalAssessment: value.ExternalAssessment,
  69.                             ConcreteExercise: value.ConcreteExercise,
  70.                             GlobalDetailed: value.GlobalDetailed,
  71.                             Comments: value.Comments,
  72.                             IntensityLevel: value.IntensityLevel,
  73.                             IntensityDuring: value.IntensityDuring,
  74.                             IntensityCustom: value.IntensityCustom,
  75.                             UserID: value.UserID,
  76.  
  77.                         };
  78.  
  79.                     }
  80.  
  81.                 });
  82.  
  83.             res.status(200).json({ result: result });
  84.         }
  85.     })
  86. };
  87.  
  88. // TODO: Needs an authenticated user.
  89. functions.createProgram = (req, res, next) => {
  90.  
  91.     logger.info('POST /api/programs called');
  92.  
  93.     // Declare the incoming program
  94.     const program = req.body;
  95.    
  96.  
  97.     const query = `INSERT INTO Program(Title, Description, Author, ISBN, Publisher, YearOfPublication, PlaceOfPublication, AddressPublisher, PhoneNumberOfPublisher, CostOfProgram, WebsitePublisher, AgeRange, InclusionCriteria, ExclusionCriteria, AlternativeTargetGroups, ScientificBasis, UnderlyingTheory, EmpiricallyTested, Efficiency, TrainingProgram, ViewOnImpairement, ViewOnCare, ViewOnQualityOfCare, PotrayalOfMankind, BuiltInAssessment, ExternalAssessment, ConcreteExercise, GlobalDetailed, Comments, IntensityCustom, UserID)` +
  98.         `VALUES('${program.Title}','${program.Description}', '${program.Author}', '${program.ISBN}', '${program.Publisher}','${program.YearOfPublication}', '${program.PlaceOfPublication}','${program.AddressPublisher}', '${program.PhoneNumberOfPublisher}','${program.CostOfProgram}','${program.WebsitePublisher}','${program.AgeRange}', '${program.InclusionCriteria}','${program.ExclusionCriteria}','${program.AlternativeTargetGroups}','${program.ScientificBasis}','${program.UnderlyingTheory}','${program.EmpiricallyTested}','${program.Efficiency}','${program.TrainingProgram}', '${program.ViewOnImpairement}','${program.ViewOnCare}', '${program.ViewOnQualityOfCare}', '${program.PotrayalOfMankind}','${program.BuiltInAssessment}','${program.ExternalAssessment}','${program.ConcreteExercise}','${program.GlobalDetailed}','${program.Comments}','${program.IntensityCustom}','${program.UserID}'); SELECT ProgramID FROM Program WHERE ProgramID = SCOPE_IDENTITY();`
  99.  
  100.    
  101.  
  102.     database.executeQuery(query, (err, rows) => {
  103.         if (err) {
  104.             const errorObject = {
  105.                 message: 'There went something wrong in the database.',
  106.                 code: 500
  107.             };
  108.             return next(errorObject);
  109.         }
  110.         if (rows) {
  111.        
  112.             console.log(rows)
  113.            
  114.             let languageQuery = `INSERT INTO ProgramLanguage(ProgramID, LanguageID)` +
  115.             `VALUES`;
  116.  
  117.             for (let i = 0; i < program.Languages.length; i++) {
  118.                 ('${program.ProgramID}, ${program.Languages} ')
  119.                 languageQuery += '(' + rows.recordset[0].ProgramID + ',' + program.Languages[i] + ')';
  120.                 if (i < program.Languages.length - 1) {
  121.                     languageQuery += ',';
  122.                 }
  123.             }
  124.            
  125.  
  126.             let SituationQuery = `INSERT INTO ProgramSituation(ProgramId, SituationId)` +
  127.                 `VALUES`;
  128.          
  129.             for (let i = 0; i < program.Situation.length; i++) {
  130.                 ('${program.ProgramID}, ${program.Situation} ')
  131.                 SituationQuery += '(' + rows.recordset[0].ProgramID + ',' + program.Situation[i] + ')';
  132.                 if (i < program.Situation.length - 1) {
  133.                     SituationQuery += ',';
  134.                 }
  135.             }
  136.  
  137.             let ImpairmentQuery = `INSERT INTO ProgramImpairement(ProgramId, ImpairementId)` +
  138.                 `VALUES`;
  139.            
  140.             for (let i = 0; i < program.Impairement.length; i++) {
  141.                 ('${program.ProgramID}, ${program.ImpairmentId} ')
  142.                 ImpairmentQuery += '(' + rows.recordset[0].ProgramID + ',' + program.Impairement[i] + ')';
  143.                 if (i < program.Situation.length - 1) {
  144.                     ImpairmentQuery += ',';
  145.                 }
  146.             }
  147.  
  148.             let PractitionerQuery = `INSERT INTO ProgramPractitioner(ProgramId, PractitionerId)` +
  149.                 `VALUES`;
  150.          
  151.             for (let i = 0; i < program.Practitioner.length; i++) {
  152.                 ('${program.ProgramID}, ${program.Practitioner} ')
  153.                 PractitionerQuery += '(' + rows.recordset[0].ProgramID + ',' + program.Practitioner[i] + ')';
  154.                 if (i < program.Practitioner.length - 1) {
  155.                     PractitionerQuery += ',';
  156.                 }
  157.             }
  158.  
  159.             let DevelopmentDomainQuery = `INSERT INTO ProgramDevelopmentDomain(ProgramId, DevelopmentDomainId)` +
  160.                 `VALUES`;
  161.          
  162.             for (let i = 0; i < program.DevelopmentDomain.length; i++) {
  163.                 ('${program.ProgramID}, ${program.DevelopmentDomain} ')
  164.                 DevelopmentDomainQuery += '(' + rows.recordset[0].ProgramID + ',' + program.DevelopmentDomain[i] + ')';
  165.                 if (i < program.DevelopmentDomain.length - 1) {
  166.                     DevelopmentDomainQuery += ',';
  167.                 }
  168.             }
  169.  
  170.             let TypeOfProgramQuery = `INSERT INTO ProgramTypeOfProgram(ProgramId, TypeOfProgramId)` +
  171.                 `VALUES`;
  172.          
  173.             for (let i = 0; i < program.TypeOfProgram.length; i++) {
  174.                 ('${program.ProgramID}, ${program.TypeOfProgram} ')
  175.                 TypeOfProgramQuery += '(' + rows.recordset[0].ProgramID + ',' + program.TypeOfProgram[i] + ')';
  176.                 if (i < program.TypeOfProgram.length - 1) {
  177.                 program[i] + ')';
  178.                     if (i < program.TypeOfProgram.length - 1) {
  179.                         TypeOfProgramQuery += ',';
  180.                     }
  181.                 }
  182.  
  183.             }
  184.  
  185.             const masterQuery = languageQuery + "; " + SituationQuery + "; " + ImpairmentQuery + "; " + PractitionerQuery + "; " + DevelopmentDomainQuery + "; " + TypeOfProgramQuery
  186.             database.executeQuery(masterQuery, (err, rows2)=>{
  187.                 if (err) {
  188.                     const errorObject = {
  189.                         message: 'There went something wrong in the database.',
  190.                         code: 500
  191.                     };
  192.                     return next(errorObject);
  193.                 }
  194.                 if(rows2){
  195.  
  196.                     const id = rows.recordset[0].ProgramID
  197.                     res.status(200).json({ProgramID:id})
  198.                 }
  199.             })
  200.         }
  201.         })
  202.        
  203.        
  204.  
  205.  
  206.     // TODO: Implement logic
  207.    
  208. };
  209.  
  210. // Delete program by id function
  211. functions.deleteProgramByID = (req, res, next) => {
  212.     logger.info('DELETE /api/programs aangeroepen');
  213.  
  214.     const programId = req.params.programID;
  215.  
  216.     if (isNaN(programId)) {
  217.         const errorObject = {
  218.             message: 'Please give a valid id number!',
  219.             code: 400
  220.         };
  221.         return next(errorObject);
  222.     }
  223.  
  224.     const getApartmentQuery = 'SELECT * FROM [Program] ' +
  225.         'WHERE [ProgramID] = ' + programId;
  226.  
  227.     database.executeQuery(getApartmentQuery, (err, rows) => {
  228.  
  229.         // Resolve error or result
  230.         if (err) {
  231.             const errorObject = {
  232.                 message: 'There went something wrong in the database.',
  233.                 code: 500
  234.             };
  235.             return next(errorObject);
  236.         }
  237.  
  238.         if (rows.recordset.length === 0) {
  239.             const errorObject = {
  240.                 message: 'The data for the given program does not exist!',
  241.                 code: 500
  242.             };
  243.  
  244.             return next(errorObject);
  245.         }
  246.  
  247.         const deleteQuery = `DELETE FROM Program WHERE ProgramID=${programId}`;
  248.  
  249.         database.executeQuery(deleteQuery, (deleteErr, deleteRows) => {
  250.  
  251.             // Resolve error or result
  252.             if (deleteErr) {
  253.                 const errorObject = {
  254.                     message: 'There went something wrong in the database.',
  255.                     code: 500
  256.                 };
  257.                 return next(errorObject);
  258.             }
  259.  
  260.             if (deleteRows.rowsAffected[0] === 1) {
  261.                 res.status(200).json(rows.recordset[0]);
  262.             } else {
  263.                 res.status(500).json({ result: 'Looks like something went wrong!' });
  264.             }
  265.         });
  266.     });
  267. };
  268.  
  269. module.exports = functions;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement