예제 #1
0
        public ResellersModule()
            : base("/Resellers")
        {
            Get["/"] = _ =>
                {
                    //this.RequiresAuthentication();
                    //this.RequiresClaims(new[] { "SuperAdmin" });

                    try
                    {
                        Resellers resellers = new Resellers();
                        return View["resellers.cshtml", resellers.GetAll()];
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Error = ex.Message;
                        log.ErrorFormat("Failed to retrieve resellers. Exception: {0}", ex.ToString());

                        return View["resellers.cshtml"];
                    }
                };

            Get["/{ResellerCode}"] = _ =>
                {
                    //this.RequiresAuthentication();
                    //this.RequiresClaims(new[] { "SuperAdmin" });

                    try
                    {
                        Resellers resellers = new Resellers();
                        Company foundReseller = resellers.GetReseller(_.ResellerCode);

                        return Response.AsJson(foundReseller, HttpStatusCode.OK);
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Error = ex.Message;
                        return View["resellers.cshtml", GetResellers()];
                    }
                };

            Put["/"] = _ =>
            {
                //this.RequiresAuthentication();
                //this.RequiresClaims(new[] { "SuperAdmin" });

                try
                {
                    var reseller = this.Bind<Company>();

                    Resellers resellers = new Resellers();
                    resellers.Update(reseller);

                    return View["resellers.cshtml", resellers.GetAll()];
                }
                catch (Exception ex)
                {
                    ViewBag.Error = ex.Message;
                    return View["resellers.cshtml", GetResellers()];
                }
            };

            Delete["/"] = _ =>
                {
                    try
                    {
                        string companyName = Request.Form.CompanyNameValidation;
                        if (string.IsNullOrEmpty(companyName))
                            throw new MissingFieldException("Resellers", "CompanyName");

                        string companyCode = Request.Form.CompanyCode;
                        if (string.IsNullOrEmpty(companyCode))
                            throw new MissingFieldException("Resellers", "CompanyCode");

                        Resellers resellers = new Resellers();
                        resellers.Delete(companyCode);

                        return View["resellers.cshtml", resellers.GetAll()];
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Error = ex.Message;
                        return View["resellers.cshtml", GetResellers()];
                    }
                };

            Post["/"] = _ =>
                {
                    Resellers resellers = new Resellers();

                    try
                    {
                        var data = this.Bind<Company>();
                        resellers.Create(data);

                        return View["resellers.cshtml", resellers.GetAll()];
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Error = ex.Message;
                        return View["resellers.cshtml", GetResellers()];
                    }
                };
        }