
import db from '../models';



const GroupCtrl = {

  /**
   *  - Create a user
   * @param {Object} req Request Object
   * @param {Object} res Response Object
   * @returns {void} Returns void
   */
  create(req, res) {
     db.Groups.findOrCreate(
      {where: {name: req.body.name}, defaults:
       {description: req.body.description}})
  .spread((group, created) => {
    if(!created){
      return res.status(409).send({message: "Group already exist"})
    } else {
      return res.status(200).send(group)
    }
  })
      
   },
    
    /** Add User To Group
   * @param {Object} req Request Object
   * @param {Object} res Response Object
   * @returns {object} Returns User
   */

   addUser(req, res) {
     const id = req.params.id;
     db.Users.findOne({where:{username: req.body.username}}).then(user =>{
       if(user){
         const newUserId = user.id;
         db.UserGroups.findOrCreate(
            {where: {userId: newUserId, groupId: id}, defaults:
            {userId: newUserId,
            groupId: id}})
        .spread((userGroup, created) => {
          if(!created){
            return res.status(409).send({message: "user already exist"})
          } else {
            return res.status(201).send(userGroup)
          }
        })
       }
     }).catch(e => {
       return res.status(404).send({message: "This User does not exist"});
     })
    }
}
 

export default GroupCtrl;

@@ -24,17 +24,16 @@
 els';%0A%0A%0A
-%0A
 const Gr
@@ -202,16 +202,21 @@
   create
+Group
 (req, re
@@ -252,16 +252,17 @@
 ate(
+%7B
 %0A      
-%7B
+ 
 wher
@@ -287,16 +287,24 @@
 .name%7D, 
+%0A       
 defaults
@@ -304,24 +304,26 @@
 efaults:
+ %7B
 %0A       
 %7Bdescrip
@@ -314,17 +314,18 @@
 %0A       
-%7B
+  
 descript
@@ -349,20 +349,35 @@
 cription
-%7D%7D)%0A
+%0A        %7D%7D)%0A      
   .sprea
@@ -396,24 +396,30 @@
 eated) =%3E %7B%0A
+      
     if(!crea
@@ -422,32 +422,39 @@
 created)%7B%0A      
+    
+   
 return res.statu
@@ -458,32 +458,48 @@
 atus(409).send(%7B
+%0A               
 message: %22Group 
@@ -516,24 +516,49 @@
 ist%22
+%0A              
 %7D)
+%7D
 %0A    
-%7D
+          
  else %7B%0A
     
@@ -549,24 +549,35 @@
      else %7B%0A
+           
       return
@@ -613,28 +613,42 @@
     
-%7D%0A  %7D)%0A
+          %7D%0A      
       
+%7D)
 %0A   %7D,%0A
+%0A
     
@@ -808,16 +808,23 @@
  addUser
+ToGroup
 (req, re
@@ -1078,25 +1078,16 @@
 d: id%7D, 
-defaults:
 %0A       
@@ -1091,16 +1091,25 @@
         
+defaults:
 %7BuserId:
@@ -1119,29 +1119,16 @@
 wUserId,
-%0A            
 groupId:
@@ -1362,24 +1362,116 @@
 %0A        %7D)%0A
+       %7D else %7B%0A         return res.status(404).send(%7Bmessage: %22This User does not exist%22%7D)%0A
        %7D%0A   
@@ -1482,20 +1482,21 @@
 .catch(e
+rr
  =%3E
- 
 %7B%0A      
@@ -1518,63 +1518,482 @@
 tus(
-404
+500
 ).send(
-%7Bmessage: %22This User does not exist%22%7D
+err)%0A     %7D)%0A    %7D,%0A%0A%0A    /** Retrieves all Users of a Group%0A   * @param %7BObject%7D req Request Object%0A   * @param %7BObject%7D res Response Object%0A   * @returns %7Bobject%7D Returns Group Users%0A   */%0A%0A    retrieveGroupUsers(req, res)%7B%0A      db.UserGroups.findAll(%7B%0A        where: %7B%0A          groupId: req.params.id%0A        %7D%7D).then(users =%3E %7B%0A          return res.status(200).send(users);%0A        %7D)%0A        .catch(err =%3E %7B%0A          return res.status(404).send(err
 );%0A     
 %7D)%0A 
@@ -1988,24 +1988,27 @@
 );%0A     
+ 
+  
 %7D)%0A    %7D
 %0A%7D%0A %0A%0Aex
@@ -1999,16 +1999,22 @@
 %7D)%0A    %7D
+,%0A%0A   
 %0A%7D%0A %0A%0Aex

remove some implementation into messageCtr.js