/// <summary>
        /// Updates the membership whose id is given as the first parameter to the contents of the second parameter.
        /// </summary>
        /// <param name="id">The membership id.</param>
        /// <param name="membership">The updated membership.</param>
        /// <returns>The newly modified membership.</returns>
        public MEMBERSHIP Update(int id, MEMBERSHIP membership)
        {
            var original = _unitOfWork.MembershipRepository.GetById(id);

            if (original == null)
            {
                throw new ResourceNotFoundException()
                      {
                          ExceptionMessage = "The Membership was not found."
                      };
            }

            validateMembership(membership);

            // One can only update certain fields within a membrship
            //original.BEGIN_DTE = membership.BEGIN_DTE;
            original.COMMENT_TXT = membership.COMMENT_TXT;
            //original.END_DTE = membership.END_DTE;
            original.PART_CDE = membership.PART_CDE;
            original.SESS_CDE = membership.SESS_CDE;

            _unitOfWork.Save();

            return(original);
        }
        // PUT: odata/MEMBERSHIPs(5)
        public IHttpActionResult Put([FromODataUri] decimal key, MEMBERSHIP mEMBERSHIP)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (key != mEMBERSHIP.ID)
            {
                return(BadRequest());
            }

            db.Entry(mEMBERSHIP).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MEMBERSHIPExists(key))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Updated(mEMBERSHIP));
        }
        /// <summary>
        /// Adds a new Membership record to storage. Since we can't establish foreign key constraints and relationships on the database side,
        /// we do it here by using the validateMembership() method.
        /// </summary>
        /// <param name="membership">The membership to be added</param>
        /// <returns>The newly added Membership object</returns>
        public MEMBERSHIP Add(MEMBERSHIP membership)
        {
            // validate returns a boolean value.
            validateMembership(membership);
            isPersonAlreadyInActivity(membership);

            // Get session begin date of the membership
            var sessionCode = _unitOfWork.SessionRepository.Find(x => x.SESS_CDE.Equals(membership.SESS_CDE)).FirstOrDefault();

            membership.BEGIN_DTE = (DateTime)sessionCode.SESS_BEGN_DTE;

            // The Add() method returns the added membership.
            var payload = _unitOfWork.MembershipRepository.Add(membership);

            // There is a unique constraint in the Database on columns (ID_NUM, PART_LVL, SESS_CDE and ACT_CDE)
            if (payload == null)
            {
                throw new ResourceCreationException()
                      {
                          ExceptionMessage = "There was an error creating the membership. Verify that a similar membership doesn't already exist."
                      };
            }
            _unitOfWork.Save();

            return(payload);
        }
        public IHttpActionResult Patch([FromODataUri] decimal key, Delta <MEMBERSHIP> patch)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            MEMBERSHIP mEMBERSHIP = db.MEMBERSHIPs.Find(key);

            if (mEMBERSHIP == null)
            {
                return(NotFound());
            }

            patch.Patch(mEMBERSHIP);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MEMBERSHIPExists(key))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Updated(mEMBERSHIP));
        }
        public IHttpActionResult ToggleGroupAdmin([FromBody] MEMBERSHIP membership)
        {
            if (!ModelState.IsValid || membership == null)
            {
                string errors = "";
                foreach (var modelstate in ModelState.Values)
                {
                    foreach (var error in modelstate.Errors)
                    {
                        errors += "|" + error.ErrorMessage + "|" + error.Exception;
                    }
                }
                throw new BadInputException()
                      {
                          ExceptionMessage = errors
                      };
            }
            var id = membership.MEMBERSHIP_ID;

            var result = _membershipService.ToggleGroupAdmin(id, membership);

            if (result == null)
            {
                return(NotFound());
            }
            return(Ok(result));
        }
        public IHttpActionResult Post([FromBody] MEMBERSHIP membership)
        {
            if (!ModelState.IsValid || membership == null)
            {
                string errors = "";
                foreach (var modelstate in ModelState.Values)
                {
                    foreach (var error in modelstate.Errors)
                    {
                        errors += "|" + error.ErrorMessage + "|" + error.Exception;
                    }
                }
                throw new BadInputException()
                      {
                          ExceptionMessage = errors
                      };
            }

            var result = _membershipService.Add(membership);

            if (result == null)
            {
                return(NotFound());
            }

            return(Created("memberships", membership));
        }
        // POST: odata/MEMBERSHIPs
        public IHttpActionResult Post(MEMBERSHIP mEMBERSHIP)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.MEMBERSHIPs.Add(mEMBERSHIP);
            db.SaveChanges();

            return(Created(mEMBERSHIP));
        }
Esempio n. 8
0
        /// <summary>
        /// Fetch the membership whose id is specified by the parameter
        /// </summary>
        /// <param name="id">The membership id</param>
        /// <returns>MembershipViewModel if found, null if not found</returns>
        public MEMBERSHIP GetSpecificMembership(int id)
        {
            MEMBERSHIP result = _unitOfWork.MembershipRepository.GetById(id);

            if (result == null)
            {
                throw new ResourceNotFoundException()
                      {
                          ExceptionMessage = "The Membership was not found."
                      };
            }

            return(result);
        }
        // DELETE: odata/MEMBERSHIPs(5)
        public IHttpActionResult Delete([FromODataUri] decimal key)
        {
            MEMBERSHIP mEMBERSHIP = db.MEMBERSHIPs.Find(key);

            if (mEMBERSHIP == null)
            {
                return(NotFound());
            }

            db.MEMBERSHIPs.Remove(mEMBERSHIP);
            db.SaveChanges();

            return(StatusCode(HttpStatusCode.NoContent));
        }
        private bool isPersonAlreadyInActivity(MEMBERSHIP membershipRequest)
        {
            var personAlreadyInActivity = _unitOfWork.MembershipRepository.Where(x => x.SESS_CDE == membershipRequest.SESS_CDE &&
                                                                                 x.ACT_CDE == membershipRequest.ACT_CDE && x.ID_NUM == membershipRequest.ID_NUM).Count() > 0;

            if (personAlreadyInActivity)
            {
                throw new ResourceCreationException()
                      {
                          ExceptionMessage = "The Person is already part of the activity."
                      };
            }

            return(true);
        }
        /// <summary>
        /// Switches the group-admin property of the person whose membership id is given
        /// </summary>
        /// <param name="id">The membership id.</param>
        /// <param name="membership">The corresponding membership object</param>
        /// <returns>The newly modified membership.</returns>
        public MEMBERSHIP ToggleGroupAdmin(int id, MEMBERSHIP membership)
        {
            var original = _unitOfWork.MembershipRepository.GetById(id);

            if (original == null)
            {
                throw new ResourceNotFoundException()
                      {
                          ExceptionMessage = "The Membership was not found."
                      };
            }

            validateMembership(membership);

            var isGuest = original.PART_CDE == "GUEST";

            if (isGuest)
            {
                throw new ArgumentException("A guest cannot be assigned as an admin.", "Participation Level");
            }

            var isAdmin = original.GRP_ADMIN ?? false;

            if (!isAdmin)
            {
                original.GRP_ADMIN = true;
            }
            else
            {
                original.GRP_ADMIN = false;
            }

            _unitOfWork.Save();

            return(original);
        }
        /// <summary>
        /// Helper method to Validate a membership
        /// </summary>
        /// <param name="membership">The membership to validate</param>
        /// <returns>True if the membership is valid. Throws ResourceNotFoundException if not. Exception is cauth in an Exception Filter</returns>
        private bool validateMembership(MEMBERSHIP membership)
        {
            var personExists = _unitOfWork.AccountRepository.Where(x => x.gordon_id.Trim() == membership.ID_NUM.ToString()).Count() > 0;

            if (!personExists)
            {
                throw new ResourceNotFoundException()
                      {
                          ExceptionMessage = "The Person was not found."
                      };
            }
            var participationExists = _unitOfWork.ParticipationRepository.Where(x => x.PART_CDE.Trim() == membership.PART_CDE).Count() > 0;

            if (!participationExists)
            {
                throw new ResourceNotFoundException()
                      {
                          ExceptionMessage = "The Participation was not found."
                      };
            }
            var sessionExists = _unitOfWork.SessionRepository.Where(x => x.SESS_CDE.Trim() == membership.SESS_CDE).Count() > 0;

            if (!sessionExists)
            {
                throw new ResourceNotFoundException()
                      {
                          ExceptionMessage = "The Session was not found."
                      };
            }
            var activityExists = _unitOfWork.ActivityInfoRepository.Where(x => x.ACT_CDE.Trim() == membership.ACT_CDE).Count() > 0;

            if (!activityExists)
            {
                throw new ResourceNotFoundException()
                      {
                          ExceptionMessage = "The Activity was not found."
                      };
            }

            var activitiesThisSession = RawSqlQuery <ActivityViewModel> .query("ACTIVE_CLUBS_PER_SESS_ID @SESS_CDE", new SqlParameter("SESS_CDE", SqlDbType.VarChar) { Value = membership.SESS_CDE });

            bool offered = false;

            foreach (var activityResult in activitiesThisSession)
            {
                if (activityResult.ACT_CDE.Trim() == membership.ACT_CDE)
                {
                    offered = true;
                }
            }

            if (!offered)
            {
                throw new ResourceNotFoundException()
                      {
                          ExceptionMessage = "The Activity is not available for this session."
                      };
            }


            return(true);
        }
        /// <summary>
        /// Approves the request with the specified ID.
        /// </summary>
        /// <param name="id">The ID of the request to be approved</param>
        /// <returns>The approved membership</returns>
        public MEMBERSHIP ApproveRequest(int id)
        {
            var query = _unitOfWork.MembershipRequestRepository.GetById(id);

            if (query == null)
            {
                throw new ResourceNotFoundException()
                      {
                          ExceptionMessage = "The Request was not found."
                      };
            }
            query.STATUS = Request_Status.APPROVED;

            MEMBERSHIP newMembership = new MEMBERSHIP
            {
                ACT_CDE     = query.ACT_CDE,
                ID_NUM      = query.ID_NUM,
                SESS_CDE    = query.SESS_CDE,
                PART_CDE    = query.PART_CDE,
                BEGIN_DTE   = DateTime.Now,
                COMMENT_TXT = "",
                GRP_ADMIN   = false
            };

            MEMBERSHIP created;

            var personAlreadyInActivity = _unitOfWork.MembershipRepository.Where(x => x.SESS_CDE == query.SESS_CDE &&
                                                                                 x.ACT_CDE == query.ACT_CDE && x.ID_NUM == query.ID_NUM);

            // If the person is already in the activity, we simply change his or her role
            if (personAlreadyInActivity.Count() > 0)
            {
                created = personAlreadyInActivity.First();

                if (created == null)
                {
                    throw new ResourceNotFoundException()
                          {
                              ExceptionMessage = "There was an error creating the membership."
                          };
                }

                // Simply change role and comment
                created.COMMENT_TXT = newMembership.COMMENT_TXT;
                created.PART_CDE    = newMembership.PART_CDE;
            }
            // Else, we add him or her to the activity
            else
            {
                // The add will fail if they are already a member.
                created = _unitOfWork.MembershipRepository.Add(newMembership);

                if (created == null)
                {
                    // The main reason why a membership won't be added is if a similar one (similar id_num, part_lvl, sess_cde and act_cde ) exists.
                    throw new ResourceCreationException()
                          {
                              ExceptionMessage = "There was an error creating the membership. Verify that a similar membership doesn't already exist."
                          };
                }
            }

            _unitOfWork.Save();

            return(created);
        }