Example #1
0
        public IHttpActionResult Post(NewRequestViewModelOnSubmit output)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid Data"));
            }
            Manager manager = new Manager(output);

            manager.SubmitRequest(output);
            return(Ok("Success"));
        }
        //manager
        public void insertTravelDetails(NewRequestViewModelOnSubmit outputModel)
        {
            using (var ctx = new da.KDTravelPortalDbContext())
            {
                foreach (var tr in outputModel.travelRequests)
                {
                    bool isClientFound               = false;
                    bool isTrvelTypeFound            = false;
                    bool isEmpWorkingUnderTheProject = false;
                    bool isStatusfound               = false;

                    if (ctx.TravelTypes.Any(tt => tt.TravelTypeId == tr.TravelTypeId))
                    {
                        isTrvelTypeFound = true;
                    }
                    else
                    {
                        throw new System.ArgumentException("This TravelType is not Found", "tr.TravelTypeId");
                    }
                    if (ctx.Clients.Any(c => c.ClientId == tr.ClientId))
                    {
                        isClientFound = true;
                    }
                    else
                    {
                        throw new System.ArgumentException("This Client is not Found", "tr.ClientId");
                    }
                    if (ctx.Statuses.Any(s => s.StatusId == tr.StatusId))
                    {
                        isStatusfound = true;
                    }
                    else
                    {
                        throw new System.ArgumentException("This status is not Found", "tr.StatusId");
                    }
                    if (ctx.EmployeesProjects.Any(ep => ep.Id == (from x in ctx.EmployeesProjects
                                                                  where x.ProjectId == tr.ProjectId &&
                                                                  x.EmployeeId == tr.EmployeeId
                                                                  select x.Id).FirstOrDefault()))

                    {
                        isEmpWorkingUnderTheProject = true;
                    }
                    else
                    {
                        throw new System.ArgumentException("tr.employeeId  doesnt work under tr.projectId so you cannot assign a travel request for him for this project,kindly check the table EmployeesProject to know the employees and their respective Projects", "");
                    }
                    if (isClientFound && isStatusfound && isTrvelTypeFound && isEmpWorkingUnderTheProject)
                    {
                        ctx.TravelRequests.Add(new da.TravelRequest()
                        {
                            TravelRequestName = tr.TravelRequestName,
                            StartDate         = tr.StartDate,
                            EndDate           = tr.EndDate,
                            Place             = tr.Place,
                            TravelType        = ctx.TravelTypes.FirstOrDefault(i => i.TravelTypeId == tr.TravelTypeId),

                            TravelTypeId = tr.TravelTypeId,


                            EmployeeProjectId = (from ep in ctx.EmployeesProjects
                                                 where ep.ProjectId == tr.ProjectId &&
                                                 ep.EmployeeId == tr.EmployeeId
                                                 select ep.Id).FirstOrDefault(),
                            Employee_Project = ctx.EmployeesProjects.FirstOrDefault(i => i.Id == (from ep in ctx.EmployeesProjects
                                                                                                  where ep.ProjectId == tr.ProjectId &&
                                                                                                  ep.EmployeeId == tr.EmployeeId
                                                                                                  select ep.Id).FirstOrDefault()),

                            Client   = ctx.Clients.FirstOrDefault(i => i.ClientId == tr.ClientId),
                            ClientId = tr.ClientId,
                            Status   = ctx.Statuses.FirstOrDefault(i => i.StatusId == tr.StatusId),
                            StatusId = tr.StatusId
                        });

                        //TravelType t = ctx.TravelTypes.FirstOrDefault(i => i.TravelTypeId == tr.TravelTypeId);

                        ctx.SaveChanges();
                    }
                    else
                    {
                        throw new System.ArgumentException("Null value is being assigned chk all constrains", "");
                    }
                }
            }
        }
 public void SubmitRequest(NewRequestViewModelOnSubmit outputModel)
 {
     _repo.insertTravelDetails(outputModel);
 }
 public Manager(NewRequestViewModelOnSubmit output)
 {
     _repo   = new RepositoryMethods();
     _output = output;
 }