Esempio n. 1
0
        public ResultModel Create(ProjectModel entity)
        {
            var     result = new ResultModel();
            dynamic output;

            var project_partner_list = new List <project_partnerModel>();

            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    _context.Add(entity);
                    var except_project_save = _unitOfWork.Save();
                    if (except_project_save != null)
                    {
                        throw except_project_save;
                    }
                    output = new
                    {
                        project_id = entity.id
                    };

                    if (!string.IsNullOrEmpty(entity.partner_id))
                    {
                        //新增合作廠商id 到project_partner table (註)
                        string[] partner_id_arr = entity.partner_id.Split(",");
                        foreach (var i in partner_id_arr)
                        {
                            var data = new project_partnerModel()
                            {
                                created    = DateTime.Now,
                                project_id = entity.id,
                                partner_id = Convert.ToInt32(i)
                            };

                            _repo_project_partner.Create(data);
                            project_partner_list.Add(data);
                        }

                        var except_partner_save = _unitOfWork.Save();
                        if (except_partner_save != null)
                        {
                            throw except_partner_save;
                        }

                        output = new
                        {
                            project_id         = entity.id,
                            project_partner_id = project_partner_list.Select(a => a.id)
                        };
                    }
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    result = ErrorCode.CUSTOM_ERROR(e.Message, "DB ERROR", 500, "cht");
                    return(result);
                }
            }

            result = ErrorCode.SUCCESS(output, "", "cht");
            return(result);
        }
Esempio n. 2
0
        public async Task <ErrorCodeLib.Models.ResultModel> Put([FromBody] ProjectModel model)
        {
            var result = new ErrorCodeLib.Models.ResultModel();
            //檢查project
            var existData = _repo_project.FindById(model.id);

            if (existData == null)
            {
                result = ErrorCode.DB_ERROR_NOT_FOUND(null, "id", "cht");
                return(result);
            }


            //檢查partner
            var exist_partner = new PartnerModel();

            if (!string.IsNullOrEmpty(model.partner_id))
            {
                string[] partner_id_arr = model.partner_id.Split(",");
                foreach (var i in partner_id_arr)
                {
                    try
                    {
                        exist_partner = _repo_partner.FindById(Convert.ToInt32(i));
                    }
                    catch (Exception e)
                    {
                        result = ErrorCode.PARAMETER_INVALID(null, "partner_id", "cht");
                        return(result);
                    }
                    if (exist_partner == null)
                    {
                        result = ErrorCode.DB_ERROR_NOT_FOUND(null, "partner_id", "cht");
                        return(result);
                    }
                }
            }
            model.updated = DateTime.Now;
            _repo_project.Update(model);

            if (!string.IsNullOrEmpty(model.partner_id))
            {
                var exist_project_partner_list = _repo_project_partner.ListProtoType(a => a.project_id == model.id);

                string[] partner_id_arr = model.partner_id.Split(",");
                ////刪除不在partner_id array裡的
                foreach (var k in exist_project_partner_list)
                {
                    if (!Array.Exists(partner_id_arr, a => a == k.partner_id.ToString()))
                    {
                        _repo_project_partner.Delete(k);
                    }
                }
                //增加exist_project_partner_list缺少的
                foreach (var i in partner_id_arr)
                {
                    bool isExist = false;
                    foreach (var j in exist_project_partner_list)
                    {
                        if (j.partner_id.ToString() == i)
                        {
                            isExist = true;
                        }
                    }
                    if (!isExist)
                    {
                        //新增合作廠商id (註)
                        var project_partner_data = new project_partnerModel();
                        project_partner_data.created    = DateTime.Now;
                        project_partner_data.project_id = model.id;
                        project_partner_data.partner_id = Convert.ToInt32(i);
                        _repo_project_partner.Create(project_partner_data);
                    }
                }
            }

            //最後存檔
            var except = _unitOfWork.Save();

            if (except != null)
            {
                result = ErrorCode.CUSTOM_ERROR(except.Message, "DB ERROR", 500, "cht");
                return(result);
            }

            result = ErrorCode.SUCCESS(null, "修改成功", "cht");
            return(result);
        }