コード例 #1
0
ファイル: AopNomController.cs プロジェクト: MartinBG/Gva
        public IHttpActionResult UpdateUnit(int id, UnitDO unit)
        {
            var oldUnit = this.unitOfWork.DbContext.Set<Unit>()
                .Include(e => e.UnitRelations)
                .FirstOrDefault(e => e.UnitId == id);

            if (oldUnit == null)
            {
                return NotFound();
            }

            if (!oldUnit.Version.SequenceEqual(unit.Version))
            {
                return Ok(new { err = "Съществува нова версия на служителя." });
            }

            if (unit.ParentId == unit.UnitId)
            {
                return Ok(new { err = "Не може потребител да е родител на себе си." });
            }

            try
            {
                using (var transaction = this.unitOfWork.BeginTransaction())
                {
                    bool parentChanged = false;
                    UnitRelation unitRelation = oldUnit.UnitRelations.SingleOrDefault();

                    if (unitRelation != null)
                    {
                        parentChanged = unitRelation.ParentUnitId != unit.ParentId;

                        if (parentChanged)
                        {
                            unitRelation.ParentUnit = null;
                            unitRelation.ParentUnitId = unit.ParentId;

                            if (unitRelation.ParentUnitId.HasValue)
                            {
                                Unit parent = this.unitOfWork.DbContext.Set<Unit>()
                                    .Include(e => e.UnitRelations)
                                    .FirstOrDefault(e => e.UnitId == unitRelation.ParentUnitId.Value);

                                if (parent != null && parent.UnitRelations.Any())
                                {
                                    unitRelation.RootUnit = null;
                                    unitRelation.RootUnitId = parent.UnitRelations.First().RootUnitId;
                                }
                                else
                                {
                                    return Ok(new { err = "Проблем със записа." });
                                }
                            }
                            else
                            {
                                unitRelation.RootUnit = null;
                                unitRelation.RootUnitId = oldUnit.UnitId;
                            }
                        }
                    }
                    else
                    {
                        return Ok(new { err = "Проблем със записа." });
                    }

                    oldUnit.Name = unit.Name;
                    oldUnit.UnitTypeId = unit.UnitTypeId.Value;
                    oldUnit.InheritParentClassification = unit.InheritParentClassification;
                    oldUnit.IsActive = unit.IsActive;

                    this.unitOfWork.Save();

                    //if (parentChanged)
                    //{
                    //    this.unitOfWork.Repo<Doc>().ExecSpSetUserDocs(oldUnit.UnitId);
                    //}

                    this.unitOfWork.Save();
                    transaction.Commit();

                    return Ok(new { err = "" });
                }
            }
            catch (Exception ex)
            {
                return Ok(new { err = ex.Message });
            }
        }
コード例 #2
0
ファイル: AopNomController.cs プロジェクト: MartinBG/Gva
        public IHttpActionResult GetUnit(int id)
        {
            var unit = this.unitOfWork.DbContext.Set<Unit>()
                .Include(e => e.UnitType)
                .Include(e => e.UnitRelations.Select(k => k.ParentUnit))
                .FirstOrDefault(e => e.UnitId == id);

            if (unit == null)
            {
                return NotFound();
            }

            UnitDO returnValue = new UnitDO(unit);

            return Ok(returnValue);
        }
コード例 #3
0
ファイル: AopNomController.cs プロジェクト: MartinBG/Gva
        public IHttpActionResult PostUnit(UnitDO unit)
        {
            try
            {
                using (var transaction = this.unitOfWork.BeginTransaction())
                {
                    Unit newUnit = new Unit();

                    newUnit.Name = unit.Name;
                    newUnit.UnitTypeId = unit.UnitTypeId.Value;
                    newUnit.IsActive = unit.IsActive;
                    newUnit.InheritParentClassification = unit.InheritParentClassification;
                    this.unitOfWork.DbContext.Set<Unit>().Add(newUnit);

                    UnitRelation unitRelation = new UnitRelation();
                    unitRelation.Unit = newUnit;
                    unitRelation.ParentUnitId = unit.ParentId;

                    if (unitRelation.ParentUnitId.HasValue)
                    {
                        Unit parent = this.unitOfWork.DbContext.Set<Unit>()
                            .Include(e => e.UnitRelations)
                            .FirstOrDefault(e => e.UnitId == unitRelation.ParentUnitId.Value);

                        if (parent != null && parent.UnitRelations.Any())
                        {
                            unitRelation.RootUnit = null;
                            unitRelation.RootUnitId = parent.UnitRelations.First().RootUnitId;
                        }
                        else
                        {
                            return Ok(new { err = "Проблем със записа." });
                        }
                    }
                    else
                    {
                        unitRelation.RootUnit = newUnit;
                    }

                    this.unitOfWork.DbContext.Set<UnitRelation>().Add(unitRelation);
                    this.unitOfWork.Save();

                    //this.unitOfWork.Repo<Doc>().ExecSpSetUserDocs(newUnit.UnitId);

                    transaction.Commit();

                    return Ok(new { err = "" });
                }
            }
            catch (Exception ex)
            {
                return Ok(new { err = ex.Message });
            }
        }