コード例 #1
0
        public ActionResult InsertOfficeInfo([FromBody] OfficeInfo data, string username)
        {
            try
            {
                if (data != null && username != null)
                {
                    var checkOffice = MH.GetSingleObject(officeinfo_collection, "OfficeName", data.OfficeName, "Address", data.Address).Result;
                    if (checkOffice != null)
                    {
                        var officeInfo = BsonSerializer.Deserialize <OfficeInfo>(checkOffice);
                        if (officeInfo.IsActive == true)
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "401",
                                Message = "Office with same office name and address is already added"
                            }));
                        }
                        else
                        {
                            var updateDefinition = Builders <BsonDocument> .Update.Set("IsActive", true);

                            update = MH.UpdateSingleObject(officeinfo_collection, "OfficeName", data.OfficeName, "Address", data.Address, updateDefinition);
                            var detail = officeInfo;
                            detail.IsActive = true;
                            AL.CreateLog(username, "InsertOfficeInfo", officeInfo, detail, activitylog_collection);
                            return(BadRequest(new ResponseData
                            {
                                Code = "402",
                                Message = "Office already added and its made active"
                            }));
                        }
                    }
                    else
                    {
                        #region Calculate office id
                        var getOffices = MH.GetListOfObjects(officeinfo_collection, null, null, null, null).Result;
                        if (getOffices.Count == 0)
                        {
                            data.OfficeId = "OF-1";
                        }
                        else
                        {
                            List <long> idList = new List <long>();
                            foreach (var office in getOffices)
                            {
                                OfficeInfo officeInfo  = BsonSerializer.Deserialize <OfficeInfo>(office);
                                long       seperatedId = Convert.ToInt64(officeInfo.OfficeId.Substring(officeInfo.OfficeId.LastIndexOf('-') + 1));
                                idList.Add(seperatedId);
                            }
                            var maxId = idList.Max();
                            data.OfficeId = "OF-" + (maxId + 1);
                            #endregion
                        }
                        data.IsActive = true;
                        var insert = MH.InsertNewOfficeInfo(data, officeinfoCollection);
                        if (insert == true)
                        {
                            AL.CreateLog(username, "InsertOfficeInfo", null, data, activitylog_collection);
                            return(Ok(new ResponseData
                            {
                                Code = "200",
                                Message = "Inserted",
                                Data = data
                            }));
                        }
                        else if (insert == false)
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "403",
                                Message = "Office info with same id is already added"
                            }));
                        }
                        else
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "400",
                                Message = "Failed",
                                Data = insert
                            }));
                        }
                    }
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "405",
                        Message = "Bad Request"
                    }));
                }
            }
            catch (Exception ex)
            {
                SL.CreateLog("OfficeController", "InsertOfficeInfo", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }