private int CalculateLevel(SectorDto sector)
        {
            int level  = 0;
            var parent = GetParent(sector, ref level);

            return(level);
        }
        public IActionResult Post(SectorDto model)
        {
            var sector = _mapper.Map <Sector>(model);

            _repo.Add(sector);
            if (_repo.SaveChanges())
            {
                return(Created($"api/sector/{model.Id}", _mapper.Map <SectorDto>(sector)));
            }
            return(BadRequest("sector not added"));
        }
示例#3
0
 public IActionResult AddSector(SectorDto sector)
 {
     if (ModelState.IsValid)
     {
         var isAdded = repository.Add(sector);
         if (isAdded)
         {
             return(Created("Sector", sector));
         }
     }
     return(BadRequest(ModelState));
 }
 public void Save(SectorDto dto)
 {
     if (dto.IsNew)
     {
         Sector sector = dto.MapTo <Sector>();
         this._sectorRepository.Insert(sector);
     }
     else
     {
         Sector sector = this._sectorRepository.Get(dto.Id);
         dto.MapTo(sector);
     }
 }
        private SectorDto GetParent(SectorDto child, ref int level)
        {
            SectorDto parent = child.Parent;

            if (parent == null)
            {
                return(parent);
            }
            else
            {
                ++level;
                return(GetParent(parent, ref level));
            }
        }
示例#6
0
        public List <SectorDto> GetAllSector()
        {
            var lst = repository.GetAll();
            List <SectorDto> secdto = new List <SectorDto>();

            foreach (Sector sec_ in lst)
            {
                SectorDto secd = new SectorDto();
                secd.Id         = sec_.Id;
                secd.SectorName = sec_.SectorName;
                secd.About      = sec_.About;
                secdto.Add(secd);
            }
            return(secdto);
        }
        public IActionResult Put(int id, SectorDto model)
        {
            var sector = _repo.GetSectorbyId(id);

            if (sector == null)
            {
                return(BadRequest("id not found"));
            }
            _mapper.Map(model, sector);
            _repo.Update(sector);
            if (_repo.SaveChanges())
            {
                return(Created($"api/logs/{model.Id}", _mapper.Map <SectorDto>(sector)));
            }
            return(BadRequest("sector not updated"));
        }
        public bool AddSector(SectorDto sector)
        {
            var newSector = new Sector
            {
                SectorName = sector.SectorName,
                WorkName   = sector.WorkName,
            };

            if (!_context.Sectors.Any(x => (x.SectorName == sector.SectorName)))
            {
                _context.Sectors.Add(newSector);
                _context.SaveChanges();
                return(false);
            }

            return(true);
        }
示例#9
0
        public IActionResult AddSector(SectorDto sector)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            var result = sectorService.AddSector(sector);

            if (!result)
            {
                return(BadRequest("Error saving Sector"));
            }

            //return CreatedAtRoute("GetProductById", new { id = obj.ID });
            return(StatusCode(201));
        }
示例#10
0
        public (bool, int) UpdateSectorDetails(SectorDto sec)
        {
            var existing = context.Sector.Find(sec.Id);

            if (existing == null)
            {
                return(false, 1);
            }

            var updated = Update(existing, sec);

            if (updated)
            {
                return(true, 1);
            }

            return(false, 2);
        }
示例#11
0
 public bool Update(Sector existing, SectorDto entity)
 {
     try
     {
         ICollection <Company> complst = existing.Companies;
         context.Entry(existing).CurrentValues.SetValues(entity);
         existing.Companies = complst;
         var updates = context.SaveChanges();
         if (updates > 0)
         {
             return(true);
         }
         return(false);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        public bool EditSector(SectorDto sector)
        {
            if (!_context.Sectors.Any(x => x.Id == sector.ID))
            {
                return(false);
            }

            var newSector = new Sector
            {
                SectorName = sector.SectorName,
                WorkName   = sector.WorkName,
            };

            DeleteSector(sector.ID);
            _context.Sectors.Add(newSector);
            _context.SaveChanges();

            return(true);
        }
示例#13
0
 public IActionResult PutUpdateCompanyDetails(int id, SectorDto sec)
 {
     if (ModelState.IsValid)
     {
         if (id == sec.Id)
         {
             (bool updated, int status) = repository.UpdateSectorDetails(sec);
             if (updated)
             {
                 return(Ok(sec));
             }
             else
             {
                 if (status == 1)
                 {
                     return(NotFound("Sector not found"));
                 }
             }
         }
     }
     return(BadRequest(ModelState));
 }
示例#14
0
        public void Test_Update_ShouldUpdateSector()
        {
            IRepository <Sector> repository = new SectorRepository(context);

            context.Sector.Add(GetSector());
            SectorDto s1 = new SectorDto();

            s1.Id         = 3;
            s1.SectorName = "Construction";
            s1.About      = "Construction Companies";

            //Act
            var    sec_ = repository.UpdateSectorDetails(s1);
            Sector s2   = context.Sector.Find(GetSector().Id);

            //Assert

            Console.WriteLine(sec_);
            Assert.That(s2.SectorName,
                        Is.EqualTo("Construction"),
                        "Sector List does NOT match");
        }
示例#15
0
 public bool Add(SectorDto entity)
 {
     try
     {
         bool   check = context.Database.CanConnect();
         Sector sec   = new Sector();
         sec.Id         = entity.Id;
         sec.SectorName = entity.SectorName;
         sec.About      = entity.About;
         sec.Companies  = new List <Company>();
         context.Sector.Add(sec);
         int u = context.SaveChanges();
         if (u > 0)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception) { return(false); }
 }
示例#16
0
        public PlanDetailsDto GetFullPlan(string date)
        {
            PlanDetailsDto       fullPlan    = new PlanDetailsDto();
            List <SectorPlanDto> sectorPlans = new List <SectorPlanDto>();
            List <SectorDto>     sectors     = new List <SectorDto>();

            var plan = _context.Plans.Where(x => x.Date.Date == DateTime.Parse(date).Date).ToList(); //here is list of users plan: id, userID, date, workName, sectorName, hours

            if (!plan.Any())
            {
                return(null);
            }

            fullPlan.Date  = plan.ElementAt(0).Date.ToString();
            fullPlan.Hours = plan.ElementAt(0).Hours;

            foreach (var worker in plan)
            {
                //here I create worker from data from list "plan"
                ShortUserDto user   = new ShortUserDto();
                SectorDto    sector = new SectorDto();
                user.UserID = worker.UserID;
                var nameAndSurname = _context.Users.Where(x => x.UserId == worker.UserID).FirstOrDefault();
                user.Name         = nameAndSurname.Name;
                user.Surname      = nameAndSurname.Surname;
                sector.ID         = 0;
                sector.SectorName = worker.Sector;
                sector.WorkName   = worker.WorkName;

                //if ther's no sectors or sector that worker is assigned hadn't created yet,
                //then create another sector in "sectors" list and then add it to "sectorPlans" list together with worker
                bool contains = false;
                foreach (SectorDto sec in sectors)
                {
                    if (sec.SectorName == sector.SectorName)
                    {
                        contains = true;
                    }
                }

                if (!contains) //!sectors.Contains(sector) doesnt work, i dont know why
                {
                    sectors.Add(sector);
                    sectorPlans.Add(new SectorPlanDto()
                    {
                        Sector  = sector,
                        Workers = new List <ShortUserDto>()
                        {
                            user
                        }
                    });
                }

                //if sector that worker is assigned for is created, then find index of sector that worker is assigned for on the "sectors" list
                //and add worker to "workers" list in "sectorPlans" list
                else
                {
                    int i     = 0;
                    int index = 0;
                    foreach (SectorDto sec in sectors) //may be simplified but findIndex does not work, i don't know why
                    {
                        if (sec == sector)
                        {
                            index = i;
                        }

                        i++;
                    }

                    sectorPlans.ElementAt(index).Workers.Add(user);
                }
            }

            fullPlan.Sectors = sectorPlans;
            return(fullPlan);
        }
示例#17
0
        public async Task <ActionResult> GetSectorDataAsync()
        {
            SectorDto[] data = new SectorDto[12];

            var json = await distributedCache.GetStringAsync("SectorData");

            if (string.IsNullOrEmpty(json))
            {
                var one = await context.Customers.Where(m => m.CreateTime.Month == 1 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var two = await context.Customers.Where(m => m.CreateTime.Month == 2 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var three = await context.Customers.Where(m => m.CreateTime.Month == 3 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var four = await context.Customers.Where(m => m.CreateTime.Month == 4 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var five = await context.Customers.Where(m => m.CreateTime.Month == 5 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var six = await context.Customers.Where(m => m.CreateTime.Month == 6 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var server = await context.Customers.Where(m => m.CreateTime.Month == 7 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var eight = await context.Customers.Where(m => m.CreateTime.Month == 8 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var nine = await context.Customers.Where(m => m.CreateTime.Month == 9 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var ten = await context.Customers.Where(m => m.CreateTime.Month == 10 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var eleven = await context.Customers.Where(m => m.CreateTime.Month == 11 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

                var twelve = await context.Customers.Where(m => m.CreateTime.Month == 12 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();



                if (one.Count() > 0)
                {
                    data[0] = new SectorDto {
                        Name = "1月", Value = one.Count()
                    };
                }
                if (two.Count() > 0)
                {
                    data[1] = new SectorDto {
                        Name = "2月", Value = two.Count()
                    };
                }
                if (three.Count() > 0)
                {
                    data[2] = new SectorDto {
                        Name = "3月", Value = three.Count()
                    };
                }
                if (four.Count() > 0)
                {
                    data[3] = new SectorDto {
                        Name = "4月", Value = four.Count()
                    };
                }
                if (five.Count() > 0)
                {
                    data[4] = new SectorDto {
                        Name = "5月", Value = five.Count()
                    };
                }
                if (six.Count() > 0)
                {
                    data[5] = new SectorDto {
                        Name = "6月", Value = six.Count()
                    };
                }
                if (server.Count() > 0)
                {
                    data[6] = new SectorDto {
                        Name = "7月", Value = server.Count()
                    };
                }
                if (eight.Count() > 0)
                {
                    data[7] = new SectorDto {
                        Name = "8月", Value = eight.Count()
                    };
                }
                if (nine.Count() > 0)
                {
                    data[8] = new SectorDto {
                        Name = "9月", Value = nine.Count()
                    };
                }
                if (ten.Count() > 0)
                {
                    data[9] = new SectorDto {
                        Name = "10月", Value = ten.Count()
                    };
                }
                if (eleven.Count() > 0)
                {
                    data[10] = new SectorDto {
                        Name = "11月", Value = eleven.Count()
                    };
                }
                if (twelve.Count() > 0)
                {
                    data[11] = new SectorDto {
                        Name = "12月", Value = twelve.Count()
                    };
                }

                DistributedCacheEntryOptions options = new DistributedCacheEntryOptions()
                {
                    AbsoluteExpiration = DateTime.Now.AddMinutes(10)
                };


                //序列化为字符串存到缓冲
                string jsonCache = JsonSerializer.Serialize <SectorDto[]>(data);


                await distributedCache.SetStringAsync($"SectorData", jsonCache, options);

                return(Ok(data));
            }


            data = JsonSerializer.Deserialize <SectorDto[]>(json);

            return(Ok(data));
        }
 public bool AddSector(SectorDto sector)
 {
     return(sectorsService.AddSector(sector));
 }
 public bool EditSector(SectorDto newData)
 {
     return(sectorsService.EditSector(newData));
 }
        private void GetChildren(IEnumerable <SectorDto> sectors, List <SectorDto> orderedSectors, SectorDto sector)
        {
            var children = sectors.Where(x => x.ParentId == sector.SectorId).OrderBy(x => x.SectorName).ToList();

            if (children.Any())
            {
                foreach (var item in children)
                {
                    orderedSectors.Add(item);
                    GetChildren(sectors, orderedSectors, item);
                }
            }
        }
        public bool AddSector(SectorDto sector)
        {
            var Obj = mapper.Map <Sector>(sector);

            return(repository.AddSector(Obj));
        }
        public async Task <ActionResult> GetSectorDataAsync()
        {
            var one = await context.Customers.Where(m => m.CreateTime.Month == 1 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var two = await context.Customers.Where(m => m.CreateTime.Month == 2 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var three = await context.Customers.Where(m => m.CreateTime.Month == 3 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var four = await context.Customers.Where(m => m.CreateTime.Month == 4 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var five = await context.Customers.Where(m => m.CreateTime.Month == 5 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var six = await context.Customers.Where(m => m.CreateTime.Month == 6 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var server = await context.Customers.Where(m => m.CreateTime.Month == 7 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var eight = await context.Customers.Where(m => m.CreateTime.Month == 8 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var nine = await context.Customers.Where(m => m.CreateTime.Month == 9 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var ten = await context.Customers.Where(m => m.CreateTime.Month == 10 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var eleven = await context.Customers.Where(m => m.CreateTime.Month == 11 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            var twelve = await context.Customers.Where(m => m.CreateTime.Month == 12 && m.IsDel == 0 && m.IsReal == 1).ToListAsync();

            SectorDto[] data = new SectorDto[12];
            data[0] = new SectorDto {
                Name = "1月", Value = one.Count()
            };
            data[1] = new SectorDto {
                Name = "2月", Value = two.Count()
            };
            data[2] = new SectorDto {
                Name = "3月", Value = three.Count()
            };
            data[3] = new SectorDto {
                Name = "4月", Value = four.Count()
            };
            data[4] = new SectorDto {
                Name = "5月", Value = five.Count()
            };
            data[5] = new SectorDto {
                Name = "6月", Value = six.Count()
            };
            data[6] = new SectorDto {
                Name = "7月", Value = server.Count()
            };
            data[7] = new SectorDto {
                Name = "8月", Value = eight.Count()
            };
            data[8] = new SectorDto {
                Name = "9月", Value = nine.Count()
            };
            data[9] = new SectorDto {
                Name = "10月", Value = ten.Count()
            };
            data[10] = new SectorDto {
                Name = "11月", Value = eleven.Count()
            };
            data[11] = new SectorDto {
                Name = "12月", Value = twelve.Count()
            };
            return(Ok(data));
        }
示例#23
0
 public static SectorDto Build(string sectorName = "sector", int value = 1, int level = 1, int?parentId = null, int sectorId = 1, SectorDto parent = null, bool isActive = true)
 {
     return(new SectorDto()
     {
         IsActive = isActive,
         Level = level,
         Parent = parent,
         ParentId = parentId,
         SectorId = sectorId,
         SectorName = sectorName,
         Value = value
     });
 }