Example #1
0
        public IHttpActionResult GetGroupById(int id)
        {
            IEnumerable <string> headerValues = Request.Headers.GetValues("Authorization");
            string installKey = headerValues.FirstOrDefault();

            if (installKey == "")
            {
                return(ResponseMessage(Models.Errors.InstallKeyBlank(Request)));
            }

            try
            {
                Models.Installs.checkinInstall(installKey);
            }
            catch (Exception ex)
            {
                return(ResponseMessage(Models.Errors.InstallCheckinFailed(Request)));
            }

            MednaNetAPIClient.Models.Groups group = null;

            try
            {
                using (Models.MedLaunchChatEntities db = new Models.MedLaunchChatEntities())
                {
                    var install = (from q in db.installs
                                   where q.code == installKey
                                   select q).FirstOrDefault();

                    if (install != null)
                    {
                        group = (from q in db.groups
                                 where q.id == id
                                 select new MednaNetAPIClient.Models.Groups()
                        {
                            groupDescription = q.group_description,
                            groupName = q.group_name,
                            groupOwner = q.group_owner,
                            id = q.id
                        }).FirstOrDefault();
                    }

                    return(Ok(group));
                }
            }
            catch (Exception ex)
            {
                HttpError err = new HttpError("Error retrieving group. ");
                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.InternalServerError, err)));
            }
        }
Example #2
0
        public IHttpActionResult CreateGroup(MednaNetAPIClient.Models.Groups group)
        {
            var APIReturn = new Models.APIReturn();

            IEnumerable <string> headerValues = Request.Headers.GetValues("Authorization");
            string installKey = headerValues.FirstOrDefault();

            if (installKey == "")
            {
                return(ResponseMessage(Models.Errors.InstallKeyBlank(Request)));
            }

            try
            {
                Models.Installs.checkinInstall(installKey);
            }
            catch (Exception ex)
            {
                return(ResponseMessage(Models.Errors.InstallCheckinFailed(Request)));
            }

            try
            {
                using (Models.MedLaunchChatEntities db = new Models.MedLaunchChatEntities())
                {
                    var install = (from q in db.installs
                                   where q.code == installKey
                                   select q).FirstOrDefault();

                    if (install != null)
                    {
                        Models.group newGroup = new Models.group();
                        newGroup.group_description = group.groupDescription;
                        newGroup.group_name        = group.groupName;
                        newGroup.group_owner       = install.id;

                        if (newGroup.group_name.Length > 20)
                        {
                            newGroup.group_name = newGroup.group_name.Substring(0, 19);
                        }

                        db.groups.Add(newGroup);

                        try
                        {
                            db.SaveChanges();
                            APIReturn.returnMessage = "Group created.";
                        }
                        catch (Exception e)
                        {
                            APIReturn.returnMessage = "Could not create group.";
                            return(new System.Web.Http.Results.ExceptionResult(e, this));
                        }

                        Models.group_members groupMember = new Models.group_members();
                        groupMember.awaiting_invite_confrim = false;
                        groupMember.group_id   = newGroup.id;
                        groupMember.install_id = install.id;
                        db.group_members.Add(groupMember);


                        try
                        {
                            db.SaveChanges();
                            APIReturn.returnMessage += "| Install added to group";
                        }
                        catch (Exception e)
                        {
                            APIReturn.returnMessage = "Could not add install to group.";
                            return(new System.Web.Http.Results.ExceptionResult(e, this));
                        }
                    }
                    else
                    {
                        APIReturn.returnMessage = "Install not found";
                    }
                }
            }
            catch (Exception ex)
            {
                HttpError err = new HttpError("Error creating group. ");
                return(ResponseMessage(Request.CreateResponse(HttpStatusCode.InternalServerError, err)));
            }

            return(Ok());
        }