Exemple #1
0
        public ClientModule(IMyContext ctx) : base("/clients")
        {
            Get["/list"] = parameters =>
            {
                var model = new ResponseBean()
                {
                    Clients = ctx.Clients.ToList()
                };
                return(View["ListClients", model]);
            };

            Get["/create"] = parameters =>
            {
                return(View["CreateClient"]);
            };

            Post["/create"] = _ =>
            {
                var message       = "Successfully added client ";
                var createSuccess = "alert-success";
                try
                {
                    var client = this.Bind <Client>();
                    client.CreationDate = DateTime.Now;

                    ctx.Clients.Add(client);
                    ctx.SaveChanges();
                    message += " " + client.CompanyName;
                }
                catch (Exception ex)
                {
                    if (ex is ModelBindingException)
                    {
                        message = "Couldn't bind model. <br>" +
                                  HtmlUtil.FormatExceptionHtml(ex);
                        createSuccess = "alert-warning";
                    }
                    else if (ex is DbUpdateException || ex is RequestExecutionException)
                    {
                        message = "Couldn't complete request to DataBase. <br>" +
                                  HtmlUtil.FormatExceptionHtml(ex);
                        createSuccess = "alert-danger";
                    }
                    else
                    {
                        message = "Something went wrong while creating your project :/<br>" +
                                  HtmlUtil.FormatExceptionHtml(ex);

                        createSuccess = "alert-danger";
                    }
                }
                var model = new ResponseBean()
                {
                    Message = message, CreateSuccess = createSuccess, Redirect = true
                };
                return(View["CreateClient", model]);
            };

            Get["/{id}"] = _ =>
            {
                ResponseBean model = new ResponseBean();
                Client       c;
                int          clientId = _.id;
                if ((c = ctx.Clients.Find(clientId)) != null)
                {
                    model.Client = c;
                    var projects = ctx.Projects.Where(p => p.Client.Id == clientId).ToList();
                    model.Projects = projects;
                    model.Success  = true; // We need to send a bool, since the SSVE @If can only check on bools
                }
                else
                {
                    model.Message = "Couldn't find client with id " + _.id;
                    model.Success = false;
                }
                return(View["ClientDetail", model]);
            };
        }
        public ProjectModule(IMyContext ctx)
            : base("/projects")
        {
            Get["/list"] = parameters =>
            {
                var model = new ResponseBean(ctx.Projects.ToList());
                return(View["ListProjects", model]);
            };

            Get["/create"] = parameters =>
            {
                var clients = new ResponseBean()
                {
                    Clients = ctx.Clients.ToList()
                };
                return(View["CreateProject", clients]);
            };

            Post["/create"] = _ =>
            {
                var message       = "Successfully added project ";
                var createSuccess = "alert-success";
                try
                {
                    string reqString = this.Request.Body.ReadAsString();
                    int    clientId  = getClientIdFromRequest(reqString);
                    var    client    = ctx.Clients.Find(clientId);
                    var    project   = this.Bind <Project>("Client");
                    project.CreationDate = DateTime.Now;
                    project.Client       = client;

                    ctx.Projects.Add(project);
                    ctx.SaveChanges();
                    message += project.Title;
                }
                catch (Exception ex)
                {
                    if (ex is ModelBindingException)
                    {
                        message = "Couldn't bind model. <br>" +
                                  HtmlUtil.FormatExceptionHtml(ex);
                        createSuccess = "alert-warning";
                    }
                    else if (ex is DbUpdateException || ex is RequestExecutionException)
                    {
                        message = "Couldn't complete request to DataBase. <br>" +
                                  HtmlUtil.FormatExceptionHtml(ex);
                        createSuccess = "alert-danger";
                    }
                    else
                    {
                        message = "Something went wrong while creating your project :/<br>" +
                                  HtmlUtil.FormatExceptionHtml(ex);

                        createSuccess = "alert-danger";
                    }
                }
                var model = new ResponseBean()
                {
                    Message       = message,
                    CreateSuccess = createSuccess,
                    Redirect      = true,
                    Clients       = ctx.Clients.ToList()
                };
                return(View["CreateProject", model]);
            };

            Get["/{id}"] = _ =>
            {
                ResponseBean model = new ResponseBean();
                Project      p;
                int          projectId = _.id;
                if ((p = ctx.Projects.Find(projectId)) != null)
                {
                    model.Project = p;
                    model.Client  = p.Client;
                    var   timeEntries = ctx.TimeEntries.Where(t => t.Project.Id == projectId).ToList();
                    float hoursSpent  = timeEntries.Sum(t => t.HoursSpent);
                    model.WorkDone   = timeEntries;
                    model.HoursSpent = hoursSpent;
                    model.Success    = true;  // We need to send a bool, since the SSVE @If can only check on bools
                }
                else
                {
                    model.Message = "Couldn't find project with id " + _.id;
                    model.Success = false;
                }
                return(View["ProjectDetail", model]);
            };

            Post["/del/{id}"] = x =>
            {
                var project = new Project()
                {
                    Id = x.id
                };
                ctx.Entry(project).State = EntityState.Deleted;

                ctx.SaveChanges();

                return(HttpStatusCode.OK);
            };
        }