Esempio n. 1
0
        public async Task <IHttpActionResult> PostPosition(Position position)
        {
            Position prePosition = db.Positions.Where(x => x.OwnerId == position.OwnerId).OrderBy(x => x.StartDate).FirstOrDefault();

            if (prePosition != null)
            {
                position.PreOwnerId = prePosition.TargetId;
            }
            db.Positions.Add(position);
            await db.SaveChangesAsync();

            Inventory inventory = await db.Inventories.FindAsync(position.OwnerId);

            inventory.PositionId = position.Id;
            Inventory existedInventory = await db.Inventories.FindAsync(inventory.Id);

            db.Entry(existedInventory).CurrentValues.SetValues(inventory);
            await db.SaveChangesAsync();

            string userId       = HttpContext.Current.User.Identity.Name;
            Guid   archivedById = (await db.Inventories.FirstOrDefaultAsync(x => x.UserId == userId)).Id;

            if (prePosition != null)
            {
                Position existedPrePosition = await db.Positions.FindAsync(prePosition.Id);

                prePosition.EndDate      = position.StartDate;
                prePosition.ArchivedById = archivedById;
                prePosition.ArchivedDate = prePosition.EndDate;
                db.Entry(existedPrePosition).CurrentValues.SetValues(prePosition);
            }
            await db.SaveChangesAsync();

            return(Ok(position));
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> PutItemCategory(Guid id, ItemCategory itemCategory)
        {
            ItemCategory existedItemCategory = await db.ItemCategories.FindAsync(id);

            db.Entry(existedItemCategory).CurrentValues.SetValues(itemCategory);
            await db.SaveChangesAsync();

            return(Ok(existedItemCategory));
        }
Esempio n. 3
0
        public async Task <IHttpActionResult> PostRedirect(Redirect redirect)
        {
            db.Redirects.Add(redirect);
            await db.SaveChangesAsync();

            await db.Entry(redirect).GetDatabaseValuesAsync();

            return(Ok(redirect));
        }
Esempio n. 4
0
        public async Task <IHttpActionResult> PostAttribute(Entities.Attribute attribute)
        {
            db.Attributes.Add(attribute);
            await db.SaveChangesAsync();

            await db.Entry(attribute).GetDatabaseValuesAsync();

            return(Ok(attribute));
        }
Esempio n. 5
0
        public async Task <IHttpActionResult> PostInventoryAttributeWith(InventoryAttribute inventoryAttribute)
        {
            db.InventoryAttributes.Add(inventoryAttribute);
            await db.SaveChangesAsync();

            await db.Entry(inventoryAttribute).GetDatabaseValuesAsync();

            return(Ok(inventoryAttribute));
        }
Esempio n. 6
0
        /// <summary>
        /// 更新【角色】
        /// </summary>
        /// <param name="id">角色序號</param>
        /// <param name="role">角色</param>
        public async Task <IHttpActionResult> PutRole(string id, AspNetRole role)
        {
            AspNetRole existedRole = await db.AspNetRoles.Where(a => a.Id == id).AsQueryable().FirstOrDefaultAsync();

            db.Entry(existedRole).CurrentValues.SetValues(role);
            await db.SaveChangesAsync();

            return(Ok());
        }
Esempio n. 7
0
        public async Task <IHttpActionResult> PostCondition(Condition condition)
        {
            db.Conditions.Add(condition);
            await db.SaveChangesAsync();

            await db.Entry(condition).GetDatabaseValuesAsync();

            return(Ok(condition));
        }
Esempio n. 8
0
        public async Task <IHttpActionResult> PostSegmentation(Segmentation segmentation)
        {
            segmentation.Owner = await db.Inventories.FindAsync(segmentation.OwnerId);

            db.Segmentations.Add(segmentation);
            await db.SaveChangesAsync();

            return(Ok(segmentation));
        }
Esempio n. 9
0
        /// <summary>
        /// 更新【畫廊】
        /// </summary>
        /// <param name="id">畫廊序號</param>
        /// <param name="folderFile">畫廊</param>
        public async Task <IHttpActionResult> PutFolderFile(Guid id, FolderFile folderFile)
        {
            FolderFile existedFolderFile = await db.FolderFiles.FindAsync(id);

            db.Entry(existedFolderFile).CurrentValues.SetValues(folderFile);
            await db.SaveChangesAsync();

            await db.Entry(existedFolderFile).GetDatabaseValuesAsync();

            return(Ok(existedFolderFile));
        }
Esempio n. 10
0
        private async Task <Inventory> CreateInventory(AspNetUser aspNetUser)
        {
            db.Inventories.Add(new Inventory("d5b63e24-1d67-4983-8803-1cb2bd80a2be")
            {
                UserId = aspNetUser.Id,
                No     = aspNetUser.UserName
            });
            await db.SaveChangesAsync();

            return(await db.Inventories.FirstOrDefaultAsync(x => x.UserId == aspNetUser.Id && x.No == aspNetUser.UserName));
        }
Esempio n. 11
0
        public async Task <IHttpActionResult> PutActivity(Guid id, Activity activity)
        {
            Activity existedActivity = await db.Activities.FindAsync(id);

            db.Entry(existedActivity).CurrentValues.SetValues(activity);
            await db.SaveChangesAsync();

            await db.Entry(existedActivity).GetDatabaseValuesAsync();

            return(Ok(existedActivity));
        }
Esempio n. 12
0
        public async Task <IHttpActionResult> PutOption(Guid id, Option option)
        {
            Option existedOption = await db.Options.FindAsync(id);

            option.Id = existedOption.Id;
            db.Entry(existedOption).CurrentValues.SetValues(option);
            await db.SaveChangesAsync();

            await db.Entry(existedOption).GetDatabaseValuesAsync();

            return(Ok(existedOption));
        }
Esempio n. 13
0
        public async Task <IHttpActionResult> PutProcess(Guid id, Process process)
        {
            Process existedProcess = await db.Processes.FindAsync(id);

            process.Id = existedProcess.Id;
            db.Entry(existedProcess).CurrentValues.SetValues(process);
            await db.SaveChangesAsync();

            await db.Entry(existedProcess).GetDatabaseValuesAsync();

            return(Ok(existedProcess));
        }
Esempio n. 14
0
        public async Task <IHttpActionResult> PutCategory(Guid id, Category category)
        {
            Category existedCategory = await db.Categories.FindAsync(id);

            category.Id = existedCategory.Id;
            db.Entry(existedCategory).CurrentValues.SetValues(category);
            await db.SaveChangesAsync();

            await db.Entry(existedCategory).GetDatabaseValuesAsync();

            return(Ok(existedCategory));
        }
Esempio n. 15
0
        /// <summary>
        /// 更新【使用者】
        /// </summary>
        /// <param name="id">使用者序號</param>
        /// <param name="userModel">使用者</param>
        public async Task <IHttpActionResult> PutUser(string id, UserModel userModel)
        {
            AspNetUser user = await db.AspNetUsers.FindAsync(id);

            AspNetUser existedUser = await db.AspNetUsers.FindAsync(id);

            if (existedUser == null)
            {
                return(NotFound());
            }
            user.UserName = userModel.Value;
            user.Id       = userModel.Id;
            user.Email    = userModel.Email;
            if (userModel.Password != "")
            {
                user.PasswordHash = repository.getHashedNewPassword(userModel.Password);
            }
            db.Entry(existedUser).CurrentValues.SetValues(user);

            foreach (UserRoleModel userRoleModel in userModel.Roles)
            {
                if (existedUser.Roles.FirstOrDefault(role => role.RoleId == userRoleModel.RoleId) == null)
                {
                    db.AspNetUserRoles.Add(new AspNetUserRole {
                        RoleId = userRoleModel.RoleId, UserId = userRoleModel.UserId
                    });
                }
            }
            foreach (AspNetUserRole existedUserRole in existedUser.Roles.ToArray())
            {
                if (userModel.Roles.FirstOrDefault(role => role.RoleId == existedUserRole.RoleId) == null)
                {
                    db.AspNetUserRoles.Remove(existedUserRole);
                }
            }
            await db.SaveChangesAsync();

            await db.Entry(existedUser).GetDatabaseValuesAsync();

            return(Ok(new UserModel
            {
                Id = existedUser.Id,
                Email = existedUser.Email,
                Password = "",
                Value = existedUser.UserName,
                Roles = existedUser.Roles.Select(x => new UserRoleModel
                {
                    RoleId = x.RoleId,
                    UserId = x.UserId,
                    RoleValue = db.Roles.Find(x.RoleId).Name
                }).ToList()
            }));
        }
Esempio n. 16
0
        public async Task <IHttpActionResult> PutWorkFlow(Guid id, WorkFlow workFlow)
        {
            WorkFlow existedWorkFlow = await db.WorkFlows.FindAsync(id);

            workFlow.Id = existedWorkFlow.Id;
            db.Entry(existedWorkFlow).CurrentValues.SetValues(workFlow);
            await db.SaveChangesAsync();

            await db.Entry(existedWorkFlow).GetDatabaseValuesAsync();

            return(Ok(existedWorkFlow));
        }
Esempio n. 17
0
        public async Task <IHttpActionResult> PutFile(Guid id, Entities.File file)
        {
            Entities.File existedFile = await db.Files.FindAsync(id);

            if (existedFile == null)
            {
                return(NotFound());
            }
            db.Entry(existedFile).CurrentValues.SetValues(file);
            await db.SaveChangesAsync();

            await db.Entry(existedFile).GetDatabaseValuesAsync();

            return(Ok(existedFile));
        }
Esempio n. 18
0
        public async Task <IHttpActionResult> PostCategoryAttribute(CategoryAttribute categoryAttribute)
        {
            Entities.Attribute attribute = db.Attributes.FirstOrDefault(x => x.Value == categoryAttribute.Target.Value && x.Code == categoryAttribute.Target.Code);

            if (attribute != null)
            {
                categoryAttribute.Target   = null;
                categoryAttribute.TargetId = attribute.Id;
            }
            db.CategoryAttributes.Add(categoryAttribute);
            await db.SaveChangesAsync();

            await db.Entry(categoryAttribute).GetDatabaseValuesAsync();

            return(Ok(categoryAttribute));
        }
Esempio n. 19
0
        public async Task <IHttpActionResult> PostWorkOrder(InventoryWithAttributes workOrderWithAttributes)
        {
            ICollection <InventoryAttribute> inventoryAttributes = workOrderWithAttributes.Attributes;
            Guid      recipeId = Guid.Parse(inventoryAttributes.FirstOrDefault(x => x.TargetId == Guid.Parse("a8ba7e62-8feb-4285-aca4-ed571de603e2")).Value);
            Inventory recipe   = db.Inventories.Find(recipeId);
            Guid      mainOutput_attributeId = Guid.Parse("1873a981-eed6-4188-97de-30bcc08a3f77");//主產品
            Item      outputItem             = await FindItem(recipe.Id, mainOutput_attributeId);

            List <Inventory> workOrders = new List <Inventory>();

            if (outputItem != null)
            {
                Guid    quantity_attributeId = Guid.Parse("28597e59-ce72-4ff4-8a79-676d3546b13e");//產量(工單)
                decimal?outputNumber         = decimal.Parse(inventoryAttributes.FirstOrDefault(x => x.TargetId == Guid.Parse("28597e59-ce72-4ff4-8a79-676d3546b13e")).Value);
                workOrders = await InsertWorkOrder(outputItem, outputNumber, 1, workOrderWithAttributes.Id);
            }
            decimal?max = 0;

            foreach (Inventory workOrder in workOrders)
            {
                Guid    sort_attributeId = Guid.Parse("e2c306dd-e5b0-46ff-9964-ad36312fb8ac");
                decimal?sort             = decimal.Parse(await SelectAttributeValueByAttributeId(workOrder.Id, sort_attributeId));
                if (sort > max)
                {
                    max = sort;
                }
            }
            max++;
            foreach (Inventory workOrder in workOrders)
            {
                Guid               sort_attributeId = Guid.Parse("e2c306dd-e5b0-46ff-9964-ad36312fb8ac");
                decimal?           newSort          = max - decimal.Parse(await SelectAttributeValueByAttributeId(workOrder.Id, sort_attributeId));
                InventoryAttribute sort             = db.InventoryAttributes.FirstOrDefault(x => x.OwnerId == workOrder.Id && x.TargetId == sort_attributeId);
                sort.Value = newSort.ToString();
                InventoryAttribute existedSort = db.InventoryAttributes.Find(sort.Id);
                db.Entry(existedSort).CurrentValues.SetValues(sort);
            }
            await db.SaveChangesAsync();

            return(Ok(db.Inventories.Where(x => x.Id == workOrderWithAttributes.Id).Select(x => new
            {
                id = x.Id,
                no = x.No,
                value = x.Value - (db.Segmentations.Where(y => y.OwnerId == x.Id).Sum(y => y.Quantity) ?? 0),
                photo = x.Photo != null ? x.Photo.Target.Path : x.Item.Photo.Target.Path,
                itemId = x.ItemId,
                itemValue = x.Item.Value,
                positionId = x.PositionId,
                positionTargetId = x.Position.TargetId,
                positionTargetNo = x.Position.Target.No,
                positionCreatedById = x.Position.CreatedById,
                positionCreatedByNo = x.Position.CreatedBy.No,
                positionPreOwnerId = x.Position.PreOwnerId,
                positionPreOwnerNo = x.Position.PreOwner.No,
                positionStartDate = x.Position.StartDate
            }).FirstOrDefault(x => x.id == workOrderWithAttributes.Id)));
        }
Esempio n. 20
0
        public async Task <IHttpActionResult> PostWorkPlan(DTOWorkPlan dto)
        {
            WorkPlan workPlan = new WorkPlan
            {
                ItemId         = dto.ItemId,
                WorkPlanRecord = new WorkPlanRecord
                {
                    Value  = dto.Value,
                    UnitId = dto.UnitId
                }
            };

            db.WorkPlans.Add(workPlan);
            await db.SaveChangesAsync();

            Item item = await db.Items.FindAsync(dto.ItemId);

            int sum = await InsertWorkOrder(workPlan.Id.ToString(), item, 1);

            return(Ok(sum));
        }
Esempio n. 21
0
        public async Task <IHttpActionResult> PostRecipe(RecipeOption recipeOption)
        {
            Inventory recipe = new Inventory("520934b7-82ed-457e-992f-1bb0cfd3749f")
            {
                No = recipeOption.Item.Value
            };

            db.Inventories.Add(recipe);
            InventoryAttribute mainOutput = new InventoryAttribute("1873a981-eed6-4188-97de-30bcc08a3f77")
            {
                OwnerId = recipe.Id,
                Value   = recipeOption.Item.Id.ToString()
            };

            db.InventoryAttributes.Add(mainOutput);
            await db.SaveChangesAsync();

            return(Ok(recipe));
        }
Esempio n. 22
0
        public async Task <Activity> CreateActivities(Process process)
        {
            foreach (Step step in db.Steps.Where(x => x.OwnerId == process.WorkFlowId).OrderBy(x => x.Sort))
            {
                if (step.Sort == (int)step.Sort)
                {
                    db.Activities.Add(new Activity
                    {
                        // ReEdit = process.ReEdit,
                        OwnerId  = process.Id,
                        TargetId = step.Id
                    });
                }
            }
            await db.SaveChangesAsync();

            Activity activity = await db.Activities.FirstOrDefaultAsync(x => x.TargetId == process.WorkFlow.FirstId);

            return(activity);
        }
Esempio n. 23
0
        public async Task <IHttpActionResult> PutParticipate(Guid id, Participate participate)
        {
            Participate currentParticipate = await db.Participates.FindAsync(id);

            Activity currentActivity = await db.Activities.FindAsync(currentParticipate.OwnerId);

            Activity nextActivity = null;
            Step     currentStep  = await db.Steps.FindAsync(currentActivity.TargetId);

            Step    nextStep       = null;
            Process currentProcess = await db.Processes.FindAsync(currentActivity.OwnerId);

            //先確認這個參與者將用哪個RedirectId過關
            Guid?   redirectId                 = null;
            decimal?redirectConditionCount     = 0;
            decimal?redirectConditionTrueRatio = 0;

            foreach (Redirect redirect in db.Redirects.Where(x => x.OwnerId == currentStep.Id).ToList().OrderBy(x => x.Target.Sort))
            {
                redirectConditionCount = 0;
                foreach (RedirectCondition redirectCondition in db.RedirectConditions.Where(x => x.OwnerId == redirect.Id).ToList().OrderBy(x => x.Sort))
                {
                    foreach (Judgment judgment in db.Judgments.Where(x => x.OwnerId == participate.Id).ToList())//取得所有判斷
                    {
                        if (judgment.ConditionId == redirectCondition.TargetId)
                        {
                            redirectConditionCount++;//條件符合即加一分
                        }
                    }
                    if (redirectConditionCount / db.RedirectConditions.Where(x => x.OwnerId == redirect.Id).Count() >= redirect.TrueRatio)//條件符合分數是否有達到原Step之Redirect通過比例
                    {
                        if (redirectConditionCount / db.RedirectConditions.Where(x => x.OwnerId == redirect.Id).Count() > redirectConditionTrueRatio)
                        {
                            redirectConditionTrueRatio = redirectConditionCount / db.RedirectConditions.Where(x => x.OwnerId == redirect.Id).Count();//有通過即設為最大通過比例 直到下一個更大通過比例的Redirect來取代
                            redirectId = redirect.Id;
                        }
                    }
                }
            }

            //取得Redirect後我們來設定下一關卡
            if (redirectId != null)
            {
                participate.RedirectId   = redirectId;           //設定通過的參與的通過原因
                participate.ArchivedDate = participate.EndDate;
                nextStep = db.Redirects.Find(redirectId).Target; //設定通過的參與的通過原因所指定的下個步驟

                //  int? ReEdit = currentActivity.ReEdit;//nextStep回關 ReEdit+1
                //  if (nextStep.Sort <= currentStep.Sort) ReEdit = ReEdit + 1;

                db.Entry(currentParticipate).CurrentValues.SetValues(participate);//送出此人的參與後
                await db.SaveChangesAsync();

                decimal?participateCount = 0; //總參與數量
                decimal?stepSubmitRatio  = 0; //所有參與者通過比例
                //接下來要判斷此活動是否所有參與者都通過要進入下一節點 //先取得所有其他參與者且RedirectId等於目前的RedirectId
                foreach (Participate otherParticipate in db.Participates.Where(x => x.OwnerId == participate.OwnerId && participate.RedirectId == redirectId && x.ArchivedDate != null).ToList())
                {
                    participateCount++;
                    if (participateCount / db.Participates.Where(x => x.OwnerId == participate.OwnerId).Count() >= currentStep.SubmitRatio)
                    {
                        if (participateCount / db.Participates.Where(x => x.OwnerId == participate.OwnerId).Count() > stepSubmitRatio)
                        {
                            stepSubmitRatio = participateCount / db.Participates.Where(x => x.OwnerId == participate.OwnerId).Count();
                        }
                    }
                }

                if (stepSubmitRatio >= currentStep.SubmitRatio)//所有參與者通過比例 大於 活動設定比率 表示 全數通過 即送出
                {
                    nextActivity = db.Activities.FirstOrDefault(x =>
                                                                x.OwnerId == currentProcess.Id &&
                                                                x.TargetId == nextStep.Id
                                                                );


                    if (nextActivity == null)//原活動列表上沒有的新活動 加入新活動
                    {
                        nextActivity = new Activity
                        {
                            OwnerId  = currentProcess.Id,
                            TargetId = nextStep.Id,
                            //  ReEdit = ReEdit,//給 General找版本用
                            PreviousId = currentActivity.Id
                        };
                        db.Activities.Add(nextActivity);
                        //   await db.SaveChangesAsync();
                        db.SaveChanges();
                    }



                    //更新目前活動
                    Activity existedCurrentActivity = await db.Activities.FindAsync(currentActivity.Id);

                    if (currentActivity.StartDate == null)
                    {
                        currentActivity.StartDate = participate.ArchivedDate;
                    }
                    currentActivity.EndDate               = participate.ArchivedDate;
                    currentActivity.ArchivedDate          = currentActivity.EndDate;
                    currentActivity.NextId                = nextActivity.Id;
                    currentActivity.ArchivedParticipateId = participate.Id;//因此 參與 而 結束了 此活動
                    db.Entry(existedCurrentActivity).CurrentValues.SetValues(currentActivity);
                    await db.SaveChangesAsync();

                    //更新目前流程
                    Process existedProcess = await db.Processes.FindAsync(currentProcess.Id);

                    // currentProcess.ReEdit = ReEdit;
                    currentProcess.PreviousId = existedProcess.CurrentId;
                    currentProcess.CurrentId  = nextActivity.Id;
                    db.Entry(existedProcess).CurrentValues.SetValues(currentProcess);
                    await db.SaveChangesAsync();

                    //更新下個活動
                    Activity existedNextActivity = await db.Activities.FindAsync(nextActivity.Id);

                    nextActivity.StartDate = currentActivity.EndDate;
                    // nextActivity.ReEdit = ReEdit;
                    nextActivity.PreviousId = currentActivity.Id;
                    db.Entry(existedNextActivity).CurrentValues.SetValues(nextActivity);

                    //增加下個活動的參與
                    //先判斷nextStep是任務還是指派
                    if (nextStep.Type.Value == "指派")
                    {
                        //群組指派
                        foreach (StepGroup stepGroup in db.StepGroups.Where(x => x.OwnerId == nextStep.Id).ToList().OrderBy(x => x.Sort))
                        {
                            foreach (Relationship inventoryGroup in db.Relationships.Where(x => x.TargetId == stepGroup.TargetId).ToList().OrderBy(x => db.InventoryAttributes.FirstOrDefault(y => y.OwnerId == x.Id && y.Target.Value == "Sort").Value))
                            {
                                Participate newParticipate = new Participate
                                {
                                    OwnerId       = nextActivity.Id,
                                    ParticipantId = inventoryGroup.OwnerId
                                };
                                db.Participates.Add(newParticipate);
                            }
                        }

                        //關卡關係人指派
                        foreach (StepRelationship stepRelationship in db.StepRelationships.Where(x => x.OwnerId == nextStep.Id).ToList().OrderBy(x => x.Sort))
                        {
                            Inventory participant = null;
                            Activity  refActivity = db.Activities.FirstOrDefault(x =>
                                                                                 x.OwnerId == currentActivity.OwnerId &&
                                                                                 x.TargetId == stepRelationship.OfId
                                                                                 );
                            if (refActivity != null)
                            {
                                Inventory    relationshipOwner = refActivity.ArchivedParticipate.Participant;
                                Relationship refRelationship   = db.Relationships.FirstOrDefault(x =>
                                                                                                 x.OwnerId == relationshipOwner.Id &&  //誰的 ex:李芳賢的
                                                                                                 x.TypeId == stepRelationship.TargetId //誰 ex:代理人
                                                                                                 );
                                if (refRelationship != null)
                                {
                                    participant = refRelationship.Target;
                                }
                                else
                                {
                                    participant = refActivity.ArchivedParticipate.Participant;
                                }
                            }



                            //給新活動 綁定 新參與 和 參與人
                            Participate newParticipate = new Participate
                            {
                                OwnerId       = nextActivity.Id,
                                ParticipantId = participant.Id
                            };
                            db.Participates.Add(newParticipate);
                        }
                        await db.SaveChangesAsync();
                    }
                    else
                    {
                        //使用者在前端接任務時才寫入newParticipate
                    }
                }
            }
            await db.SaveChangesAsync();

            await db.Entry(currentParticipate).GetDatabaseValuesAsync();

            return(Ok(currentParticipate));
        }
Esempio n. 24
0
        public async Task <IHttpActionResult> PutItem(Guid id, Item item)
        {
            HashSet <Category> categories = new HashSet <Category>();

            if (item.Description != null)
            {
                string[] description = item.Description.Split('#');
                foreach (string _categoryValue in description)
                {
                    string categoryValue = _categoryValue.Trim();
                    if (categoryValue != "" && categories.Count(x => x.Value == categoryValue) == 0)
                    {
                        Category category = await db.Categories.FirstOrDefaultAsync(x => x.Value == categoryValue.Trim());

                        if (category == null)
                        {
                            category = new Category {
                                Value = categoryValue.Trim()
                            };
                            db.Categories.Add(category);
                            await db.SaveChangesAsync();
                        }
                        categories.Add(category);
                    }
                }
            }

            foreach (Category category in categories)
            {
                ItemCategory itemCategory = await db.ItemCategories.FirstOrDefaultAsync(x => x.OwnerId == id && x.TargetId == category.Id);

                if (itemCategory == null)
                {
                    db.ItemCategories.Add(new ItemCategory
                    {
                        StartDate = DateTime.Now,
                        OwnerId   = item.Id,
                        TargetId  = category.Id
                    });
                    await db.SaveChangesAsync();
                }
            }
            foreach (ItemCategory itemCategory in db.ItemCategories.Where(x => x.OwnerId == id).ToArray())
            {
                Category category = categories.FirstOrDefault(x => x.Id == itemCategory.TargetId);
                if (category == null)
                {
                    db.ItemCategories.Remove(itemCategory);
                }
            }
            Item existedItem = await db.Items.FindAsync(id);

            if (item.PhotoId == null)
            {
                item.PhotoId = existedItem.PhotoId;
            }
            db.Entry(existedItem).CurrentValues.SetValues(item);
            await db.SaveChangesAsync();

            return(Ok(db.Items.Where(x => x.Id == item.Id).Select(x => new
            {
                id = x.Id,
                value = x.Value,
                photo = x.Photo.Target.Path,
                code = x.Code,
                description = x.Description,
                deletable = (db.Inventories.Where(y => y.ItemId == x.Id).Count() == 0),
                unit = new { no = x.Unit.No },
                unitId = x.UnitId,
                unitTypeId = x.UnitTypeId
            }).FirstOrDefault()));
        }