Esempio n. 1
0
        public HttpResponseMessage GetBoardOfProject(string ProjectID, string BoardID) // 抓指定白板資料
        {
            var response = new JsonResponse();
            var data     = (from Board in db.Boards.Where(b => b.ProjectID == ProjectID && b.BoardID == BoardID && b.DeleteAt == null)
                            select new
            {
                ProjectID = Board.ProjectID,
                BoardID = Board.BoardID,
                Code = Board.Code
            }).FirstOrDefault();

            if (data == null)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Resource not found !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 404

                return(response.Get());
            }

            response.Set(new
            {
                Success = true,
                Message = "資源已取得!!",
                Data    = data
            }, HttpStatusCode.OK); // Http Status Code: 200

            return(response.Get());
        }
Esempio n. 2
0
        public HttpResponseMessage ChangeName(string ProjectID, string name)
        {
            var response = new JsonResponse();
            var session  = ChoySession.Current;
            var MemberID = session.LoginId;

            if (MemberID == null)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "您無權限進行此操作 !!"
                }, HttpStatusCode.Unauthorized); // Http Status Code: 401

                return(response.Get());
            }

            var data = db.Projects
                       .Where(p => p.ProjectID == ProjectID && p.MemberID == MemberID && p.DeleteAt == null)
                       .FirstOrDefault();

            if (data == null)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Resource not found !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 404

                return(response.Get());
            }

            try
            {
                data.ProjectName     = name;
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            catch (System.Exception)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Update failed !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 200

                return(response.Get());
            }

            response.Set(new
            {
                Success = true,
                Message = "Update completed !!",
            }, HttpStatusCode.OK); // Http Status Code: 200

            return(response.Get());
        }
Esempio n. 3
0
        // public HttpResponseMessage Patch(ApiBoardPatch request)
        public HttpResponseMessage Update(string ProjectID, string BoardID, ApiBoardUpdate request)
        {
            var response = new JsonResponse();

            if (request.Canvas == null)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "缺少執行所需參數 !!"
                }, HttpStatusCode.BadRequest); // Http Status Code: 400

                return(response.Get());
            }

            var data = db.Boards
                       .Where(b => b.ProjectID == ProjectID && b.BoardID == BoardID)
                       .FirstOrDefault();

            if (data == null)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Resource not found !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 404

                return(response.Get());
            }

            if (ModelState.IsValid)
            {
                data.Code            = request.Canvas;
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();

                response.Set(new
                {
                    Success = true,
                    Message = "Update completed !!"
                }, HttpStatusCode.OK); // Http Status Code: 200

                return(response.Get());
            }

            response.Set(new
            {
                Success = false,
                Message = "Update failed !!"
            }, HttpStatusCode.NotFound); // Http Status Code: 200

            return(response.Get());
        }
Esempio n. 4
0
        public HttpResponseMessage Update(int VoteID)
        {
            var response = new JsonResponse();
            var data     = db.Votes.Where(v => v.VoteID == VoteID).FirstOrDefault();
            var result   = db.Votes.Where(v => v.VoteID == VoteID).FirstOrDefault().Result;

            if (data == null)
            { // not found
                response.Set(new
                {
                    Success = false,
                    Message = "Resource not found !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 404

                return(response.Get());
            }

            if (result != null)
            {
                response.Set(new
                {
                    Success = true,
                    Message = "此投票已結束"
                }, HttpStatusCode.Accepted); // Http Status Code: 202

                return(response.Get());
            }

            if (ModelState.IsValid)
            { // 更新成功
                data.VoteCount       = data.VoteCount - 1;
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();

                response.Set(new
                {
                    Success = true,
                    Message = "Update completed !!"
                }, HttpStatusCode.OK); // Http Status Code: 200

                return(response.Get());
            }
            // 更新失敗
            response.Set(new
            {
                Success = false,
                Message = "Update failed !!"
            }, HttpStatusCode.NotFound); // Http Status Code: 200

            return(response.Get());
        }
Esempio n. 5
0
        public HttpResponseMessage GetAllBoardsOfSharedProjects()
        {
            var response = new JsonResponse();
            var session  = ChoySession.Current;
            var MemberID = session.LoginId;

            var data = (
                from jp in db.MemberJoinProjects
                from p in db.Projects
                where
                jp.MemberIDJoin == MemberID &&
                jp.ProjectID == p.ProjectID &&
                p.DeleteAt == null &&
                jp.MemberIDOwner != MemberID
                select new
            {
                ProjectID = p.ProjectID,
                ProjectName = p.ProjectName,
                Data = (
                    from Board in db.Boards.Where(b => b.ProjectID == p.ProjectID)
                    select new
                {
                    ProjectID = Board.ProjectID,
                    BoardID = Board.BoardID,
                    Code = Board.Code
                }).ToList()
            }).ToList();

            if (data.Count == 0)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Resource not found !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 404

                return(response.Get());
            }

            response.Set(new
            {
                Success = true,
                Message = "資源已取得!!",
                Data    = data
            }, HttpStatusCode.OK); // Http Status Code: 200

            return(response.Get());
        }
Esempio n. 6
0
        public HttpResponseMessage Get()
        {
            JsonResponse Response = new JsonResponse(true, new SecureContext());

            Response.Create();

            if (Response.IsAuthentic == true)
            {
                List <Area> Output = new List <Area>();

                var Source = Umbraco.Content(1122).Children().Where("Visible");

                if (Source.Count() > 0)
                {
                    foreach (var Node in Source)
                    {
                        Output.Add(new Area {
                            ID    = Node.Id,
                            Title = Node.GetPropertyValue("title")
                        });
                    }
                }

                Response.Set(new StringContent(JsonConvert.SerializeObject(Output), ApiContext.GetEncoding(), ApiContext.GetOutputType()));
            }

            return(Response.Get());
        }
        public HttpResponseMessage Get()
        {
            JsonResponse Response = new JsonResponse(true, new SecureContext());

            Response.Create();

            if (Response.IsAuthentic == true)
            {
                List <Post> Output = new List <Post>();

                var Source = Umbraco.Content(1125).Children().Where("Visible");

                if (Source.Count() > 0)
                {
                    foreach (var Node in Source)
                    {
                        Output.Add(new Post
                        {
                            ID          = Node.Id,
                            Title       = (string)Node.GetPropertyValue("title"),
                            Body        = (string)Node.GetPropertyValue("body"),
                            PublishedOn = Node.CreateDate,
                            PublishedBy = (string)Node.CreatorName,
                            Image       = "https://www.jirikralovec.cz/dev/givskud/images/demo-animalimage.jpg"
                                          // Image = "https://" + HttpContext.Current.Request.Url.Host + Node.GetPropertyValue("image").Url,
                        });
                    }
                }

                Response.Set(new StringContent(JsonConvert.SerializeObject(Output), ApiContext.GetEncoding(), ApiContext.GetOutputType()));
            }

            return(Response.Get());
        }
        public HttpResponseMessage Post([FromBody] SeasonPass Data)
        {
            JsonResponse Response = new JsonResponse(true, new SecureContext());

            Response.Create();

            if (Response.IsAuthentic == true)
            {
                IContentService Service = Services.ContentService;

                var Pass = Service.CreateContent(EncDecService.Hash(Data.ID), 1121, "seasonPass");
                Pass.SetValue("passID", EncDecService.Hash(Data.ID));
                Pass.SetValue("holderName", EncDecService.Encrypt(Data.Holder));
                Pass.SetValue("validFrom", Convert.ToDateTime(Data.ValidFrom));
                Pass.SetValue("validTo", Convert.ToDateTime(Data.ValidTo));
                Pass.SetValue("acquiredOn", Convert.ToDateTime(Data.AcquiredOn));

                string ResponseMessage;

                if (Service.SaveAndPublishWithStatus(Pass))
                {
                    ResponseMessage = "The content has been successfully saved.";
                }
                else
                {
                    ResponseMessage = "An error occured while saving the season pass.";
                }

                Response.Set(new StringContent(ResponseMessage, ApiContext.GetEncoding(), "text/plain"));
            }

            return(Response.Get());
        }
Esempio n. 9
0
        public HttpResponseMessage Update(string GroupID, string name)
        {
            var response = new JsonResponse();

            var session  = ChoySession.Current;
            var MemberID = session.LoginId;

            var data = db.Groups
                       .Where(g => g.GroupID == GroupID && g.MemberID == MemberID)
                       .FirstOrDefault();

            if (data == null)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Resource not found !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 404

                return(response.Get());
            }

            try
            {
                data.GroupName       = name;
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            catch (System.Exception)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Update failed !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 200

                return(response.Get());
            }

            response.Set(new
            {
                Success = true,
                Message = "Update completed !!"
            }, HttpStatusCode.OK); // Http Status Code: 200

            return(response.Get());
        }
Esempio n. 10
0
        public HttpResponseMessage Create(string ProjectID)
        {
            var response = new JsonResponse();
            var session  = ChoySession.Current;
            var MemberID = session.LoginId;
            var data     = db.Projects.Where(p => p.ProjectID == ProjectID && p.DeleteAt == null).FirstOrDefault();

            if (data == null)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Resource not found !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 404

                return(response.Get());
            }
            try
            {
                db.Boards.Add(new Board
                {
                    ProjectID     = data.ProjectID,
                    MemberIDOwner = data.MemberID
                });
                db.SaveChanges();
            }
            catch
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Create failed !!"
                }, HttpStatusCode.InternalServerError); // Http Status Code: 500

                return(response.Get());
            }

            response.Set(new
            {
                Success = true,
                Message = "Create completed !!"
            }, HttpStatusCode.OK); // Http Status Code: 200


            return(response.Get());
        }
Esempio n. 11
0
        // public HttpResponseMessage Patch(ApiBoardPatch request)
        public HttpResponseMessage Delete(string ProjectID, string BoardID)
        {
            var response = new JsonResponse();
            var data     = db.Boards
                           .Where(b => b.ProjectID == ProjectID && b.BoardID == BoardID && b.DeleteAt == null)
                           .FirstOrDefault();

            if (data == null)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Resource not found !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 404

                return(response.Get());
            }

            try
            {
                data.DeleteAt        = DateTime.Now;
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            catch
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Delete failed !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 200

                return(response.Get());
            }

            response.Set(new
            {
                Success = true,
                Message = "Delete completed !!"
            }, HttpStatusCode.OK); // Http Status Code: 200

            return(response.Get());
        }
Esempio n. 12
0
        public HttpResponseMessage GetAllVotasOfProject(string ProjectID)
        {
            var response = new JsonResponse();

            var data = (from Vote in db.Votes.Where(v => v.ProjectID == ProjectID)
                        select new
            {
                VoteID = Vote.VoteID,
                VoteName = Vote.VoteName,
                Result = Vote.Result,
                Choices = (from VoteRecord in db.VoteRecords.Where(vr => vr.VoteID == Vote.VoteID)
                           select new
                {
                    ChoiceID = VoteRecord.ChoiceID,
                    Choice = VoteRecord.Choice,
                    VoteCounts = VoteRecord.VoteCounts
                }).ToList()
            }).ToList();

            if (data.Count == 0)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Resource not found !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 404

                return(response.Get());
            }

            response.Set(new
            {
                Success = true,
                Message = "資源已成功取得!!",
                Data    = data
            }, HttpStatusCode.OK); // Http Status Code: 200

            return(response.Get());
        }
        public HttpResponseMessage Get()
        {
            SecureContext SC            = new SecureContext();
            string        PassParameter = SC.GetHeaderParam("PassID");

            JsonResponse Response = new JsonResponse(true, SC);

            Response.Create();

            if (Response.IsAuthentic == true)
            {
                List <SeasonPass> Output = new List <SeasonPass>();

                var Source = Umbraco.Content(1121).Children().Where("Visible");

                if (Source.Count() > 0)
                {
                    foreach (var Node in Source)
                    {
                        if ((string)Node.GetPropertyValue("passID") != PassParameter)
                        {
                            continue;
                        }
                        else
                        {
                            Output.Add(new SeasonPass
                            {
                                ID         = "n/a",
                                Holder     = (string)Node.GetPropertyValue("holderName"),
                                ValidFrom  = Convert.ToDateTime(Node.GetPropertyValue("validFrom")).ToString("dd-MM-yyyy"),
                                ValidTo    = Convert.ToDateTime(Node.GetPropertyValue("validTo")).ToString("dd-MM-yyyy"),
                                AcquiredOn = Convert.ToDateTime(Node.GetPropertyValue("acquiredOn")).ToString("dd-MM-yyyy")
                            });
                        }
                    }
                }

                Response.Set(new StringContent(JsonConvert.SerializeObject(Output), ApiContext.GetEncoding(), ApiContext.GetOutputType()));
            }

            return(Response.Get());
        }
Esempio n. 14
0
        public HttpResponseMessage Get()
        {
            JsonResponse Response = new JsonResponse(true, new SecureContext());

            Response.Create();

            if (Response.IsAuthentic == true)
            {
                List <Event> Output     = new List <Event>();
                var          DataSource = Umbraco.Content(1124).Children().Where("Visible");

                if (DataSource.Count() > 0)
                {
                    foreach (var Node in DataSource)
                    {
                        Event Event = new Event
                        {
                            ID            = Node.Id,
                            Title         = Node.GetPropertyValue("title"),
                            Desc          = Node.GetPropertyValue("description"),
                            IsBoundToDate = Node.GetPropertyValue("isDateOnly"),
                            EventDate     = Node.GetPropertyValue("eventDate"),
                            EventTime     = Node.GetPropertyValue("eventTime")
                        };
                        if (Event.IsBoundToDate)
                        {
                            if (DateTime.Now != Event.EventDate)
                            {
                                continue;
                            }
                        }
                        Output.Add(Event);
                    }
                }

                Response.Set(new StringContent(JsonConvert.SerializeObject(Output), ApiContext.GetEncoding(), ApiContext.GetOutputType()));
            }

            return(Response.Get());
        }
Esempio n. 15
0
        public HttpResponseMessage Get()
        {
            JsonResponse Response = new JsonResponse(true, new SecureContext());

            Response.Create();

            if (Response.IsAuthentic == true)
            {
                List <string> Output = new List <string>();

                var Source = Umbraco.Content(0001).Children().Where("Visible");

                if (Source.Count() > 0)
                {
                    foreach (var Node in Source)
                    {
                    }
                }

                Response.Set(new StringContent(JsonConvert.SerializeObject(Output), ApiContext.GetEncoding(), ApiContext.GetOutputType()));
            }

            return(Response.Get());
        }
Esempio n. 16
0
        public HttpResponseMessage ForgetPassword(ApiAuthForgetPassword request)
        {
            var response = new JsonResponse();

            if (string.IsNullOrWhiteSpace(request.Email) || !Validator.IsValidEmail(request.Email))
            {
                response.Set(new
                {
                    Success = false,
                    Message = "信箱格式錯誤,請重新輸入 !!"
                }, HttpStatusCode.BadRequest); // Http Status Code: 400

                return(response.Get());
            }

            var db   = new ChoyContext();
            var data = db.Members.Where(m => m.Email == request.Email).FirstOrDefault();

            if (data != null)
            {
                // 產生暫時密碼
                string allowedChars   = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
                int    passwordLength = 18;//密碼長度
                char[] chars          = new char[passwordLength];
                Random rd             = new Random();
                for (int i = 0; i < passwordLength; i++)
                {
                    chars[i] = allowedChars[rd.Next(0, allowedChars.Length)]; // 隨機從 allowedChars 取得一個字
                }
                string password = new string(chars);

                var env     = new Env();
                var jws     = new SimpleJws();
                var payload = new Dictionary <string, object>();
                var exp     = DateTime.Now.AddMinutes(45);

                payload.Add("MemberID", data.MemberID);
                payload.Add("Password", password);
                payload.Add("exp", TimeConverter.ToTimestamp(exp)); // 時效 45

                string token = jws.Encode(payload, env.SecretKey);
                string link  = HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery, "/Home/SetRandenPassword");

                var client = new RestClient(env.SendForgetPassworadEmailAPI);
                client.Timeout = -1;
                var req = new RestRequest(Method.POST);
                req.AddHeader("Content-Type", "application/json");

                var content = new
                {
                    Recipient = data.ContactEmail,
                    NickName  = data.NickName,
                    Link      = link + $"?Token={token}",
                    Exp       = exp.ToString("MM/dd/yyyy HH:mm:ss"),
                    Password  = password,
                };

                req.AddParameter("application/json", JsonConvert.SerializeObject(content), ParameterType.RequestBody);
                IRestResponse res = client.Execute(req);
                // var dict_response = JsonConvert.DeserializeObject<Dictionary<string, object>>(res.Content);
                // HttpStatusCode statusCode = res.StatusCode;
                // int numericStatusCode = (int)statusCode;

                // if (!(bool)dict_response["Success"])
                // {
                //   response.Set(new
                //   {
                //     Success = false,
                //     Message = dict_response["Messages"]
                //   }, HttpStatusCode.InternalServerError); // Http Status Code: 500

                //   return response.Get();
                // }
            }
            // else
            // {
            //   response.Set(new
            //   {
            //     Success = false,
            //     Message = "此信箱尚未被註冊"
            //   }, HttpStatusCode.NotFound); // Http Status Code: 404

            //   return response.Get();
            // }

            response.Set(new
            {
                Success = true,
                Message = "如果此註冊信箱存在,暫時密碼已透過 Email 寄送,請至您於本站設定的聯絡 Email 收信 !!"
            }, HttpStatusCode.BadRequest); // Http Status Code: 400

            return(response.Get());
        }
        public HttpResponseMessage Get()
        {
            JsonResponse Response = new JsonResponse(true, new SecureContext());

            Response.Create();

            if (Response.IsAuthentic == true)
            {
                List <Quiz> Output = new List <Quiz>();

                var Source = Umbraco.Content(1123).Children().Where("Visible");

                // Sort parameter
                Int32.TryParse(HttpContext.Current.Request.QueryString["id"], out int QuizId);

                if (Source.Count() > 0)
                {
                    foreach (var Node in Source)
                    {
                        if (QuizId != 0 && Node.Id != QuizId)
                        {
                            continue;
                        }
                        Quiz Quiz = new Quiz
                        {
                            ID                = Node.Id,
                            Title             = Node.GetPropertyValue("title"),
                            IsLockedByDefault = (bool)Node.GetPropertyValue("isLockedByDefault"),
                            Questions         = new List <QuizQuestion>(),
                            Image             = "https://www.jirikralovec.cz/dev/givskud/images/demo-animalimage.jpg"
                                                // Image = "https://" + HttpContext.Current.Request.Url.Host + Node.GetPropertyValue("image").Url
                        };
                        // Individual questions
                        IEnumerable <IPublishedContent> Questions = Node.GetPropertyValue("questions");
                        foreach (IPublishedContent Q in Questions)
                        {
                            QuizQuestion QQ = new QuizQuestion
                            {
                                Question      = (string)Q.GetPropertyValue("questionText"),
                                Answers       = new Dictionary <int, string>(),
                                CorrectAnswer = (int)Q.GetPropertyValue("correctAnswer")
                            };
                            string[] Answers = (string[])Q.GetPropertyValue("answers");
                            if (Answers.Count() > 0)
                            {
                                for (int i = 0; i < Answers.Count(); i++)
                                {
                                    QQ.Answers.Add(i, Answers[i]);
                                }
                            }
                            Quiz.Questions.Add(QQ);
                        }
                        Output.Add(Quiz);
                    }
                }

                Response.Set(new StringContent(JsonConvert.SerializeObject(Output), ApiContext.GetEncoding(), ApiContext.GetOutputType()));
            }

            return(Response.Get());
        }
        public HttpResponseMessage Get()
        {
            JsonResponse Response = new JsonResponse(true, new SecureContext());

            Response.Create();

            if (Response.IsAuthentic == true)
            {
                List <Animal> Output = new List <Animal>();

                var Source = Umbraco.Content(1126).Children().Where("Visible");

                if (Source.Count() > 0)
                {
                    Int32.TryParse(HttpContext.Current.Request.QueryString["sortbyarea"], out int AreaId);

                    foreach (var Node in Source)
                    {
                        // Sort by area
                        IPublishedContent AnimalArea = Node.GetPropertyValue <IPublishedContent>("animalAreaId");
                        if (AreaId != 0 && AnimalArea.Id != AreaId)
                        {
                            continue;
                        }
                        // Get Animal quiz
                        int AnimalQuizId = -1;
                        var AnimalQuiz   = Node.GetPropertyValue("animalQuizId");
                        if (AnimalQuiz != null)
                        {
                            IPublishedContent AnimalQuizNode = (IPublishedContent)AnimalQuiz;
                            AnimalQuizId = AnimalQuizNode.Id;
                        }

                        // Output object
                        Animal Out = new Animal
                        {
                            ID   = Node.Id,
                            Name = (string)Node.GetPropertyValue("animalName"),

                            Content = Node.GetPropertyValue <string[]>("animalContent"),

                            Height = AnimalsControllerHelper.GroupedStringContent(Node.GetPropertyValue <string[]>("animalHeight"), " - ", " cm"),
                            Length = AnimalsControllerHelper.GroupedStringContent(Node.GetPropertyValue <string[]>("animalLength"), " - ", " cm"),
                            Weight = AnimalsControllerHelper.GroupedStringContent(Node.GetPropertyValue <string[]>("animalWeight"), " - ", " kg"),

                            Descendants = AnimalsControllerHelper.GroupedStringContent(Node.GetPropertyValue <string[]>("animalDescendants"), " -  ", ""),
                            Lifetime    = AnimalsControllerHelper.GroupedStringContent(Node.GetPropertyValue <string[]>("animalLifetime"), " - ", ""),

                            PregnancyTime = Node.GetPropertyValue <string>("animalPregnancyTime"),
                            Continent     = AnimalsControllerHelper.GroupedStringContent(Node.GetPropertyValue <string[]>("animalContinent"), ", ", ""),

                            /*
                             * Icon = "https://" + HttpContext.Current.Request.Url.Host + Node.GetPropertyValue("animalIcon").Url,
                             * Image = "https://" + HttpContext.Current.Request.Url.Host + Node.GetPropertyValue("animalImage").Url,
                             */
                            Icon  = "https://www.jirikralovec.cz/dev/givskud/images/demo-animalicon.png",
                            Image = "https://www.jirikralovec.cz/dev/givskud/images/demo-animalimage.jpg",

                            Status  = Node.GetPropertyValue <string>("animalStatus"),
                            Eats    = Node.GetPropertyValue <string>("animalEats"),
                            Species = Node.GetPropertyValue("animalSpecies"),

                            AreaID = AnimalArea.Id,
                            QuizID = AnimalQuizId
                        };
                        Output.Add(Out);
                    }
                }

                Response.Set(new StringContent(JsonConvert.SerializeObject(Output), ApiContext.GetEncoding(), ApiContext.GetOutputType()));
            }

            return(Response.Get());
        }
Esempio n. 19
0
        public HttpResponseMessage Create(string ProjectID, ApiVoteCreate request)
        {
            var response = new JsonResponse();

            if (request.VoteName == null || request.Choices.Length < 2 || request.VoteCount == null)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "缺少執行所需參數 !!"
                }, HttpStatusCode.BadRequest); // Http Status Code: 400

                return(response.Get());
            }

            var data = db.Projects.Where(p => p.ProjectID == ProjectID).FirstOrDefault();

            if (data == null)
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Resource not found !!"
                }, HttpStatusCode.NotFound); // Http Status Code: 404

                return(response.Get());
            }

            var vote = new Vote
            {
                VoteName      = request.VoteName,
                ProjectID     = data.ProjectID,
                MemberIDOwner = data.MemberID,
                VoteCount     = request.VoteCount
            };

            db.Votes.Add(vote);

            for (int i = 0; i < request.Choices.Length; i++)
            {
                VoteRecords voteRecords = new VoteRecords
                {
                    Choice = request.Choices[i]
                };
                db.VoteRecords.Add(voteRecords);
            }

            try
            {
                db.SaveChanges();
            }
            catch
            {
                response.Set(new
                {
                    Success = false,
                    Message = "Create failed !!"
                }, HttpStatusCode.InternalServerError); // Http Status Code: 500

                return(response.Get());
            }

            response.Set(new
            {
                Success = true,
                Message = "Create completed !!",
                Data    = vote.VoteID
            }, HttpStatusCode.OK); // Http Status Code: 200

            return(response.Get());
        }