Esempio n. 1
0
        public async Task <IHttpActionResult> Updatestaff()
        {
            ResponseDataDTO <staff> response = new ResponseDataDTO <staff>();

            try
            {
                var path = Path.GetTempPath();

                if (!Request.Content.IsMimeMultipartContent("form-data"))
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
                }

                MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(path);

                await Request.Content.ReadAsMultipartAsync(streamProvider);

                // save file
                string fileName = "";
                if (streamProvider.FileData.Count > 0)
                {
                    foreach (MultipartFileData fileData in streamProvider.FileData)
                    {
                        fileName = FileExtension.SaveFileOnDisk(fileData);
                    }
                }
                if (streamProvider.FormData["sta_fullname"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Họ và tên không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["sta_username"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Tên đăng nhập không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["sta_password"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Mật khẩu không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }

                if (streamProvider.FormData["sta_mobile"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Số điện thoại không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }

                if (streamProvider.FormData["sta_status"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Trạng thái không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }

                if (streamProvider.FormData["department_id"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Phòng ban không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }


                if (streamProvider.FormData["position_id"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Chức vụ không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }

                // get data from formdata

                StaffUpdateViewModel staffUpdateViewModel = new StaffUpdateViewModel
                {
                    sta_id = Convert.ToInt32(streamProvider.FormData["sta_id"]),

                    sta_fullname = Convert.ToString(streamProvider.FormData["sta_fullname"]),
                    sta_code     = Convert.ToString(streamProvider.FormData["sta_code"]),
                    sta_username = Convert.ToString(streamProvider.FormData["sta_username"]),
                    sta_password = Convert.ToString(streamProvider.FormData["sta_password"]),
                    sta_email    = Convert.ToString(streamProvider.FormData["sta_email"]),

                    sta_aboutme            = Convert.ToString(streamProvider.FormData["sta_aboutme"]),
                    sta_mobile             = Convert.ToString(streamProvider.FormData["sta_mobile"]),
                    sta_identity_card      = Convert.ToString(streamProvider.FormData["sta_identity_card"]),
                    sta_address            = Convert.ToString(streamProvider.FormData["sta_address"]),
                    sta_hometown           = Convert.ToString(streamProvider.FormData["sta_hometown"]),
                    sta_reason_to_end_work = Convert.ToString(streamProvider.FormData["sta_reason_to_end_work"]),
                    sta_note = Convert.ToString(streamProvider.FormData["sta_note"]),

                    department_id   = Convert.ToInt32(streamProvider.FormData["department_id"]),
                    group_role_id   = Convert.ToInt32(streamProvider.FormData["group_role_id"]),
                    social_id       = Convert.ToInt32(streamProvider.FormData["social_id"]),
                    position_id     = Convert.ToInt32(streamProvider.FormData["position_id"]),
                    sta_leader_flag = Convert.ToByte(streamProvider.FormData["sta_leader_flag"]),



                    sta_status = Convert.ToByte(streamProvider.FormData["sta_status"]),
                    sta_sex    = Convert.ToByte(streamProvider.FormData["sta_sex"]),
                };


                var existstaff = _staffservice.Find(staffUpdateViewModel.sta_id);

                if (streamProvider.FormData["cu_thumbnail"] != null)
                {
                    if (fileName != "")
                    {
                        staffUpdateViewModel.sta_thumbnai = fileName;
                    }
                    else
                    {
                        staffUpdateViewModel.sta_thumbnai = existstaff.sta_thumbnai;
                    }
                }


                //md5
                if (staffUpdateViewModel.sta_email != null)
                {
                    if (CheckEmail.IsValidEmail(staffUpdateViewModel.sta_email) == false)
                    {
                        response.Message = "Định dạng email không hợp lệ !";
                        response.Data    = null;
                        return(Ok(response));
                    }
                }
                else
                {
                    staffUpdateViewModel.sta_email = null;
                }
                //check_phone_number

                if (CheckNumber.IsPhoneNumber(staffUpdateViewModel.sta_mobile) == false)
                {
                    response.Message = "Số điện thoại không hợp lệ";
                    response.Data    = null;
                    return(Ok(response));
                }
                //Ma code
                staffUpdateViewModel.sta_code = existstaff.sta_code;

                //Address
                if (streamProvider.FormData["sta_address"] == null)
                {
                    staffUpdateViewModel.sta_address = null;
                }
                // Option choose
                if (streamProvider.FormData["sta_sex"] == null)
                {
                    if (existstaff.sta_sex != null)
                    {
                        staffUpdateViewModel.sta_sex = existstaff.sta_sex;
                    }
                    else
                    {
                        staffUpdateViewModel.sta_sex = null;
                    }
                }


                //checkdatetime
                if (streamProvider.FormData["sta_birthday"] == null)
                {
                    if (existstaff.sta_birthday != null)
                    {
                        staffUpdateViewModel.sta_birthday = existstaff.sta_birthday;
                    }
                    else
                    {
                        staffUpdateViewModel.sta_birthday = null;
                    }
                }
                else
                {
                    staffUpdateViewModel.sta_birthday = Convert.ToDateTime(streamProvider.FormData["sta_birthday"]);
                }
                if (streamProvider.FormData["sta_identity_card"] == null)
                {
                    staffUpdateViewModel.sta_identity_card = null;
                }
                else
                {
                    staffUpdateViewModel.sta_identity_card = Convert.ToString(streamProvider.FormData["sta_identity_card"]);
                }

                if (streamProvider.FormData["sta_identity_card_date"] == null)
                {
                    if (existstaff.sta_identity_card_date != null)
                    {
                        staffUpdateViewModel.sta_identity_card_date = existstaff.sta_identity_card_date;
                    }
                    else
                    {
                        staffUpdateViewModel.sta_identity_card_date = null;
                    }
                }
                else
                {
                    staffUpdateViewModel.sta_identity_card_date = Convert.ToDateTime(streamProvider.FormData["sta_identity_card_date"]);
                }
                if (streamProvider.FormData["sta_end_work_date"] == null)
                {
                    if (existstaff.sta_end_work_date != null)
                    {
                        staffUpdateViewModel.sta_end_work_date = existstaff.sta_end_work_date;
                    }
                    else
                    {
                        staffUpdateViewModel.sta_end_work_date = null;
                    }
                }
                else
                {
                    staffUpdateViewModel.sta_end_work_date = Convert.ToDateTime(streamProvider.FormData["sta_end_work_date"]);
                }

                if (streamProvider.FormData["sta_start_work_date"] == null)
                {
                    if (existstaff.sta_start_work_date != null)
                    {
                        staffUpdateViewModel.sta_start_work_date = existstaff.sta_start_work_date;
                    }
                    else
                    {
                        staffUpdateViewModel.sta_start_work_date = null;
                    }
                }
                else
                {
                    staffUpdateViewModel.sta_start_work_date = Convert.ToDateTime(streamProvider.FormData["sta_start_work_date"]);
                }

                staffUpdateViewModel.sta_created_date = existstaff.sta_created_date;
                // mapping view model to entity
                var updatedstaff = _mapper.Map <staff>(staffUpdateViewModel);

                // update staff
                _staffservice.Update(updatedstaff, staffUpdateViewModel.sta_id);

                // return response
                response.Code    = HttpCode.OK;
                response.Message = MessageResponse.SUCCESS;
                response.Data    = updatedstaff;
                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                response.Message = ex.Message;
                response.Data    = null;
                Console.WriteLine(ex.ToString());

                return(Ok(response));
            }
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> Createstaff()
        {
            ResponseDataDTO <staff> response = new ResponseDataDTO <staff>();

            try
            {
                var path = Path.GetTempPath();

                if (!Request.Content.IsMimeMultipartContent("form-data"))
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
                }

                MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(path);

                await Request.Content.ReadAsMultipartAsync(streamProvider);

                // save file
                string fileName = "";
                foreach (MultipartFileData fileData in streamProvider.FileData)
                {
                    fileName = (FileExtension.SaveFileOnDisk(fileData));
                }
                #region ["Check null"]

                if (streamProvider.FormData["sta_fullname"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Họ và tên không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["sta_username"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Tên đăng nhập không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["sta_password"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Mật khẩu không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }

                if (streamProvider.FormData["sta_mobile"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Số điện thoại không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }

                if (streamProvider.FormData["sta_status"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Trạng thái không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }

                if (streamProvider.FormData["department_id"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Phòng ban không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }


                if (streamProvider.FormData["position_id"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Chức vụ không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["add_province"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Tinh/Thanh pho không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["add_district"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Quan/Huyen không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["add_ward"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Xa/Phuong không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                #endregion

                #region ["Check exits"]
                if (!check_department(Convert.ToInt32(streamProvider.FormData["department_id"])))
                {
                    response.Code    = HttpCode.NOT_FOUND;
                    response.Message = "Khong co ma phong ban trong csdl!";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (!check_position(Convert.ToInt32(Convert.ToInt32(streamProvider.FormData["position_id"]))))
                {
                    response.Code    = HttpCode.NOT_FOUND;
                    response.Message = "Khong co ma bo phan trong csdl!";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (!check_grouprole(Convert.ToInt32(streamProvider.FormData["group_role_id"])))
                {
                    response.Code    = HttpCode.NOT_FOUND;
                    response.Message = "Khong co ma nhom quyen trong csdl!";
                    response.Data    = null;
                    return(Ok(response));
                }

                #endregion
                // get data from formdata những trường bắt buộc
                StaffCreateViewModel StaffCreateViewModel = new StaffCreateViewModel
                {
                    sta_fullname = Convert.ToString(streamProvider.FormData["sta_fullname"]),
                    //sta_code = Convert.ToString(streamProvider.FormData["sta_code"]),
                    sta_username = Convert.ToString(streamProvider.FormData["sta_username"]),
                    sta_password = Convert.ToString(streamProvider.FormData["sta_password"]),
                    sta_email    = Convert.ToString(streamProvider.FormData["sta_email"]),

                    sta_aboutme            = Convert.ToString(streamProvider.FormData["sta_aboutme"]),
                    sta_mobile             = Convert.ToString(streamProvider.FormData["sta_mobile"]),
                    sta_identity_card      = Convert.ToString(streamProvider.FormData["sta_identity_card"]),
                    sta_address            = Convert.ToString(streamProvider.FormData["sta_address"]),
                    sta_hometown           = Convert.ToString(streamProvider.FormData["sta_hometown"]),
                    sta_reason_to_end_work = Convert.ToString(streamProvider.FormData["sta_reason_to_end_work"]),
                    sta_note = Convert.ToString(streamProvider.FormData["sta_note"]),

                    department_id   = Convert.ToInt32(streamProvider.FormData["department_id"]),
                    group_role_id   = Convert.ToInt32(streamProvider.FormData["group_role_id"]),
                    social_id       = Convert.ToInt32(streamProvider.FormData["social_id"]),
                    position_id     = Convert.ToInt32(streamProvider.FormData["position_id"]),
                    sta_leader_flag = Convert.ToByte(streamProvider.FormData["sta_leader_flag"]),



                    sta_status = Convert.ToByte(streamProvider.FormData["sta_status"]),
                    sta_sex    = Convert.ToByte(streamProvider.FormData["sta_sex"]),
                };
                //Kiểm tra các trường rằng buộc
                if (check_username_email(StaffCreateViewModel.sta_username, StaffCreateViewModel.sta_email))
                {
                    response.Code    = HttpCode.NOT_FOUND;
                    response.Message = "Da co username '" + StaffCreateViewModel.sta_username + "' hoac email '" + StaffCreateViewModel.sta_email + "' trong csdl";
                    response.Data    = null;
                    return(Ok(response));
                }
                //md5
                if (StaffCreateViewModel.sta_email != null)
                {
                    if (CheckEmail.IsValidEmail(StaffCreateViewModel.sta_email) == false)
                    {
                        response.Message = "Định dạng email không hợp lệ !";
                        response.Data    = null;
                        return(Ok(response));
                    }
                }

                //check_phone_number

                if (CheckNumber.IsPhoneNumber(StaffCreateViewModel.sta_mobile) == false)
                {
                    response.Message = "Số điện thoại không hợp lệ";
                    response.Data    = null;
                    return(Ok(response));
                }
                //Bắt các truongf còn lại
                //check datetime

                if (streamProvider.FormData["sta_birthday"] == null)
                {
                    StaffCreateViewModel.sta_birthday = null;
                }
                else
                {
                    StaffCreateViewModel.sta_birthday = Convert.ToDateTime(streamProvider.FormData["sta_birthday"]);
                }

                if (streamProvider.FormData["sta_identity_card_date"] == null)
                {
                    StaffCreateViewModel.sta_identity_card_date = null;
                }
                else
                {
                    StaffCreateViewModel.sta_identity_card_date = Convert.ToDateTime(streamProvider.FormData["sta_identity_card_date"]);
                }
                if (streamProvider.FormData["sta_end_work_date"] == null)
                {
                    StaffCreateViewModel.sta_end_work_date = null;
                }
                else
                {
                    StaffCreateViewModel.sta_end_work_date = Convert.ToDateTime(streamProvider.FormData["sta_end_work_date"]);
                }
                if (streamProvider.FormData["sta_start_work_date"] == null)
                {
                    StaffCreateViewModel.sta_start_work_date = null;
                }
                else
                {
                    StaffCreateViewModel.sta_start_work_date = Convert.ToDateTime(streamProvider.FormData["sta_start_work_date"]);
                }


                if (streamProvider.FormData["sta_created_date"] == null)
                {
                    StaffCreateViewModel.sta_created_date = DateTime.Now;
                }
                //Lấy ra bản ghi cuối cùng tạo mã code
                var x = _staffservice.GetLast();

                StaffCreateViewModel.sta_code = Utilis.CreateCode("KH", x.sta_id, 7);
                // mapping view model to entity
                var createdstaff = _mapper.Map <staff>(StaffCreateViewModel);
                createdstaff.sta_thumbnai = fileName;
                createdstaff.sta_password = StaffCreateViewModel.sta_password;

                // save new staff
                _staffservice.Create(createdstaff);
                var get_last_id = _staffservice.GetLast();
                //Create address
                AddressCreateViewModel addressCreateViewModel = new AddressCreateViewModel
                {
                    add_province = Convert.ToString(streamProvider.FormData["add_province"]),

                    add_district = Convert.ToString(streamProvider.FormData["add_district"]),
                    add_ward     = Convert.ToString(streamProvider.FormData["add_ward"]),
                };
                addressCreateViewModel.staff_id = get_last_id.sta_id;
                var createAddress = _mapper.Map <address>(addressCreateViewModel);
                _addressService.Create(createAddress);

                // return response
                response.Code    = HttpCode.OK;
                response.Message = MessageResponse.SUCCESS;
                response.Data    = createdstaff;
                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                response.Message = ex.Message;
                response.Data    = null;

                return(Ok(response));
            }
        }
Esempio n. 3
0
        public async Task <IHttpActionResult> Updatecustomer()
        {
            ResponseDataDTO <customer> response = new ResponseDataDTO <customer>();

            try
            {
                var path = Path.GetTempPath();

                if (!Request.Content.IsMimeMultipartContent("form-data"))
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
                }

                MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(path);

                await Request.Content.ReadAsMultipartAsync(streamProvider);

                // save file
                string fileName = "";
                if (streamProvider.FileData.Count > 0)
                {
                    foreach (MultipartFileData fileData in streamProvider.FileData)
                    {
                        fileName = FileExtension.SaveFileOnDisk(fileData);
                    }
                }
                //Cach truong bat buoc
                if (streamProvider.FormData["cu_fullname"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Họ và tên không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["cu_mobile"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Số điện thoại không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["cu_email"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Email không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["cu_type"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Loại khách hàng không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["customer_group_id"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Nhóm khách hàng không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["source_id"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Nguồn không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                // get data from formdata
                CustomerUpdateViewModel customerUpdateViewModel = new CustomerUpdateViewModel
                {
                    cu_id       = Convert.ToInt32(streamProvider.FormData["cu_id"]),
                    cu_mobile   = Convert.ToString(streamProvider.FormData["cu_mobile"]),
                    cu_email    = Convert.ToString(streamProvider.FormData["cu_email"]),
                    cu_fullname = Convert.ToString(streamProvider.FormData["cu_fullname"]),

                    customer_group_id = Convert.ToInt32(streamProvider.FormData["customer_group_id"]),
                    source_id         = Convert.ToInt32(streamProvider.FormData["source_id"]),

                    cu_type = Convert.ToByte(streamProvider.FormData["cu_type"]),
                };


                var existcustomer = _customerservice.Find(customerUpdateViewModel.cu_id);

                if (streamProvider.FormData["cu_thumbnail"] != null)
                {
                    if (fileName != "")
                    {
                        customerUpdateViewModel.cu_thumbnail = fileName;
                    }
                    else
                    {
                        customerUpdateViewModel.cu_thumbnail = existcustomer.cu_thumbnail;
                    }
                }

                //md5

                if (CheckEmail.IsValidEmail(customerUpdateViewModel.cu_email) == false && customerUpdateViewModel.cu_email == "")
                {
                    response.Message = "Định dạng email không hợp lệ !";
                    response.Data    = null;
                    return(Ok(response));
                }
                //check_phone_number
                if (CheckNumber.IsPhoneNumber(customerUpdateViewModel.cu_mobile) == false && customerUpdateViewModel.cu_mobile == "")
                {
                    response.Message = "Số điện thoại không hợp lệ";
                    response.Data    = null;
                    return(Ok(response));
                }
                //bat cac truog con lai
                if (streamProvider.FormData["cu_birthday"] == null)
                {
                    if (existcustomer.cu_birthday != null)
                    {
                        customerUpdateViewModel.cu_birthday = existcustomer.cu_birthday;
                    }
                    else
                    {
                        customerUpdateViewModel.cu_birthday = null;
                    }
                }
                else
                {
                    customerUpdateViewModel.cu_birthday = Convert.ToDateTime(streamProvider.FormData["cu_birthday"]);
                }
                if (streamProvider.FormData["cu_address"] == null)
                {
                    customerUpdateViewModel.cu_address = null;
                }
                else
                {
                    customerUpdateViewModel.cu_address = Convert.ToString(streamProvider.FormData["cu_address"]);
                }
                if (streamProvider.FormData["cu_note"] == null)
                {
                    customerUpdateViewModel.cu_note = null;
                }
                else
                {
                    customerUpdateViewModel.cu_note = Convert.ToString(streamProvider.FormData["cu_note"]);
                }
                if (streamProvider.FormData["cu_geocoding"] == null)
                {
                    customerUpdateViewModel.cu_geocoding = null;
                }
                else
                {
                    customerUpdateViewModel.cu_geocoding = Convert.ToString(streamProvider.FormData["cu_geocoding"]);
                }
                if (streamProvider.FormData["cu_curator_id"] == null)
                {
                    customerUpdateViewModel.cu_curator_id = null;
                }
                else
                {
                    customerUpdateViewModel.cu_curator_id = Convert.ToInt32(streamProvider.FormData["cu_curator_id"]);
                }
                if (streamProvider.FormData["cu_age"] == null)
                {
                    customerUpdateViewModel.cu_age = null;
                }
                else
                {
                    customerUpdateViewModel.cu_age = Convert.ToInt32(streamProvider.FormData["cu_age"]);
                }


                if (streamProvider.FormData["cu_status"] == null)
                {
                    customerUpdateViewModel.cu_status = null;
                }
                else
                {
                    customerUpdateViewModel.cu_status = Convert.ToByte(streamProvider.FormData["cu_status"]);
                }

                // mapping view model to entity
                var updatedcustomer = _mapper.Map <customer>(customerUpdateViewModel);



                // update customer
                _customerservice.Update(updatedcustomer, customerUpdateViewModel.cu_id);

                // return response
                response.Code    = HttpCode.OK;
                response.Message = MessageResponse.SUCCESS;
                response.Data    = updatedcustomer;
                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                response.Message = ex.Message;
                response.Data    = null;
                Console.WriteLine(ex.ToString());

                return(Ok(response));
            }
        }
Esempio n. 4
0
        public async Task <IHttpActionResult> UpdateCustomerOder([FromBody] CustomerOrderProductViewModelUpdate customer_order_update)
        {
            ResponseDataDTO <bool> response = new ResponseDataDTO <bool>();

            try
            {
                //Id user now
                new BaseController();
                var current_id = BaseController.current_id;
                if (customer_order_update.customer.cu_id == 0)
                {
                    #region [Create Customer]
                    //Cach truong bat buoc
                    if (customer_order_update.customer.cu_fullname == null)
                    {
                        response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                        response.Message = "Họ và tên không được để trống";
                        response.Data    = false;
                        return(Ok(response));
                    }
                    if (customer_order_update.customer.cu_mobile == null)
                    {
                        response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                        response.Message = "Số điện thoại không được để trống";
                        response.Data    = false;
                        return(Ok(response));
                    }
                    if (customer_order_update.customer.cu_email == null)
                    {
                        response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                        response.Message = "Email không được để trống";
                        response.Data    = false;
                        return(Ok(response));
                    }
                    if (customer_order_update.customer.cu_type == 0)
                    {
                        response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                        response.Message = "Loại khách hàng không được để trống";
                        response.Data    = false;
                        return(Ok(response));
                    }
                    if (customer_order_update.customer.customer_group_id == 0)
                    {
                        response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                        response.Message = "Nhóm khách hàng không được để trống";
                        response.Data    = false;
                        return(Ok(response));
                    }
                    if (customer_order_update.customer.source_id == 0)
                    {
                        response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                        response.Message = "Nguồn không được để trống";
                        response.Data    = false;
                        return(Ok(response));
                    }
                    // get data from formdata
                    CustomerCreateViewModel customerCreateViewModel = new CustomerCreateViewModel
                    {
                        cu_mobile   = Convert.ToString(customer_order_update.customer.cu_mobile),
                        cu_email    = Convert.ToString(customer_order_update.customer.cu_email),
                        cu_fullname = Convert.ToString(customer_order_update.customer.cu_fullname),

                        customer_group_id = Convert.ToInt32(customer_order_update.customer.customer_group_id),
                        source_id         = Convert.ToInt32(customer_order_update.customer.source_id),

                        cu_type = Convert.ToByte(customer_order_update.customer.cu_type),
                    };
                    //Bat cac dieu kien rang buoc
                    if (CheckEmail.IsValidEmail(customerCreateViewModel.cu_email) == false && customerCreateViewModel.cu_email == "")
                    {
                        response.Message = "Định dạng email không hợp lệ !";
                        response.Data    = false;
                        return(Ok(response));
                    }

                    if (CheckNumber.IsPhoneNumber(customerCreateViewModel.cu_mobile) == false && customerCreateViewModel.cu_mobile == "")
                    {
                        response.Message = "Số điện thoại không hợp lệ";
                        response.Data    = false;
                        return(Ok(response));
                    }


                    //bat cac truog con lai
                    if (customer_order_update.customer.cu_birthday == null)
                    {
                        customerCreateViewModel.cu_birthday = null;
                    }
                    else
                    {
                        customerCreateViewModel.cu_birthday = Convert.ToDateTime(customer_order_update.customer.cu_birthday);
                    }
                    if (customer_order_update.customer.cu_address == null)
                    {
                        customerCreateViewModel.cu_address = null;
                    }
                    else
                    {
                        customerCreateViewModel.cu_address = Convert.ToString(customer_order_update.customer.cu_address);
                    }
                    if (customer_order_update.customer.cu_note == null)
                    {
                        customerCreateViewModel.cu_note = null;
                    }
                    else
                    {
                        customerCreateViewModel.cu_note = Convert.ToString(customer_order_update.customer.cu_note);
                    }
                    if (customer_order_update.customer.cu_geocoding == null)
                    {
                        customerCreateViewModel.cu_geocoding = null;
                    }
                    else
                    {
                        customerCreateViewModel.cu_geocoding = Convert.ToString(customer_order_update.customer.cu_geocoding);
                    }
                    if (customer_order_update.customer.cu_curator_id == 0)
                    {
                        customerCreateViewModel.cu_curator_id = null;
                    }
                    else
                    {
                        customerCreateViewModel.cu_curator_id = Convert.ToInt32(customer_order_update.customer.cu_curator_id);
                    }
                    if (customer_order_update.customer.cu_age == 0)
                    {
                        customerCreateViewModel.cu_age = null;
                    }
                    else
                    {
                        customerCreateViewModel.cu_age = Convert.ToInt32(customer_order_update.customer.cu_age);
                    }
                    if (customer_order_update.customer.cu_status == 0)
                    {
                        customerCreateViewModel.cu_status = null;
                    }
                    else
                    {
                        customerCreateViewModel.cu_status = Convert.ToByte(customer_order_update.customer.cu_status);
                    }

                    customerCreateViewModel.staff_id       = Convert.ToInt32(current_id);
                    customerCreateViewModel.cu_create_date = DateTime.Now;
                    var cu = _customerservice.GetLast();
                    customerCreateViewModel.cu_code = Utilis.CreateCode("CU", cu.cu_id, 7);
                    // mapping view model to entity
                    var createdcustomer = _mapper.Map <customer>(customerCreateViewModel);

                    // save new customer
                    _customerservice.Create(createdcustomer);
                    var cu_last = _customerservice.GetLast();
                    customer_order_update.customer.cu_id = cu_last.cu_id;
                    #endregion
                }


                var existscustomerorder = _customer_orderservice.Find(customer_order_update.cuo_id);

                existscustomerorder.customer_id        = customer_order_update.customer.cu_id;
                existscustomerorder.staff_id           = Convert.ToInt32(current_id);
                existscustomerorder.cuo_payment_status = customer_order_update.cuo_payment_status;
                existscustomerorder.cuo_payment_type   = customer_order_update.cuo_payment_type;
                existscustomerorder.cuo_ship_tax       = customer_order_update.cuo_ship_tax;
                existscustomerorder.cuo_total_price    = customer_order_update.cuo_total_price;
                existscustomerorder.cuo_discount       = customer_order_update.cuo_discount;
                existscustomerorder.cuo_status         = customer_order_update.cuo_status;



                // update customer order
                _customer_orderservice.Update(existscustomerorder, existscustomerorder.cuo_id);

                //update order product

                OrderProductCreateViewModel orderCreateViewModel = new OrderProductCreateViewModel {
                };

                foreach (productorderviewmodel i in customer_order_update.list_product)
                {
                    var order_product_delete = _order_productservice.Find(i.op_id);
                    if (order_product_delete != null)
                    {
                        _order_productservice.Delete(order_product_delete);
                    }

                    orderCreateViewModel.customer_order_id = customer_order_update.cuo_id;
                    orderCreateViewModel.op_discount       = i.op_discount;
                    orderCreateViewModel.op_note           = i.op_note;
                    orderCreateViewModel.op_quantity       = i.op_quantity;
                    orderCreateViewModel.product_id        = i.product_id;

                    var createdorderproduct = _mapper.Map <order_product>(orderCreateViewModel);

                    _order_productservice.Create(createdorderproduct);
                }


                // return response
                response.Code    = HttpCode.OK;
                response.Message = MessageResponse.SUCCESS;
                response.Data    = false;
                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                response.Message = ex.Message;
                response.Data    = true;
                Console.WriteLine(ex.ToString());

                return(Ok(response));
            }
        }
Esempio n. 5
0
        public async Task <IHttpActionResult> Createcustomer()
        {
            ResponseDataDTO <customer> response = new ResponseDataDTO <customer>();

            try
            {
                var path = Path.GetTempPath();

                if (!Request.Content.IsMimeMultipartContent("form-data"))
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
                }

                MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(path);

                await Request.Content.ReadAsMultipartAsync(streamProvider);

                // save file
                string fileName = "";
                foreach (MultipartFileData fileData in streamProvider.FileData)
                {
                    fileName = (FileExtension.SaveFileOnDisk(fileData));
                }
                //Cach truong bat buoc
                if (streamProvider.FormData["cu_fullname"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Họ và tên không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["cu_mobile"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Số điện thoại không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["cu_email"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Email không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["cu_type"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Loại khách hàng không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["customer_group_id"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Nhóm khách hàng không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                if (streamProvider.FormData["source_id"] == null)
                {
                    response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                    response.Message = "Nguồn không được để trống";
                    response.Data    = null;
                    return(Ok(response));
                }
                // get data from formdata
                CustomerCreateViewModel customerCreateViewModel = new CustomerCreateViewModel
                {
                    cu_mobile   = Convert.ToString(streamProvider.FormData["cu_mobile"]),
                    cu_email    = Convert.ToString(streamProvider.FormData["cu_email"]),
                    cu_fullname = Convert.ToString(streamProvider.FormData["cu_fullname"]),

                    customer_group_id = Convert.ToInt32(streamProvider.FormData["customer_group_id"]),
                    source_id         = Convert.ToInt32(streamProvider.FormData["source_id"]),

                    cu_type = Convert.ToByte(streamProvider.FormData["cu_type"]),
                };
                //Bat cac dieu kien rang buoc
                if (CheckEmail.IsValidEmail(customerCreateViewModel.cu_email) == false && customerCreateViewModel.cu_email == "")
                {
                    response.Message = "Định dạng email không hợp lệ !";
                    response.Data    = null;
                    return(Ok(response));
                }

                if (CheckNumber.IsPhoneNumber(customerCreateViewModel.cu_mobile) == false && customerCreateViewModel.cu_mobile == "")
                {
                    response.Message = "Số điện thoại không hợp lệ";
                    response.Data    = null;
                    return(Ok(response));
                }


                //bat cac truog con lai
                if (streamProvider.FormData["cu_birthday"] == null)
                {
                    customerCreateViewModel.cu_birthday = null;
                }
                else
                {
                    customerCreateViewModel.cu_birthday = Convert.ToDateTime(streamProvider.FormData["cu_birthday"]);
                }
                if (streamProvider.FormData["cu_address"] == null)
                {
                    customerCreateViewModel.cu_address = null;
                }
                else
                {
                    customerCreateViewModel.cu_address = Convert.ToString(streamProvider.FormData["cu_address"]);
                }
                if (streamProvider.FormData["cu_note"] == null)
                {
                    customerCreateViewModel.cu_note = null;
                }
                else
                {
                    customerCreateViewModel.cu_note = Convert.ToString(streamProvider.FormData["cu_note"]);
                }
                if (streamProvider.FormData["cu_geocoding"] == null)
                {
                    customerCreateViewModel.cu_geocoding = null;
                }
                else
                {
                    customerCreateViewModel.cu_geocoding = Convert.ToString(streamProvider.FormData["cu_geocoding"]);
                }
                if (streamProvider.FormData["cu_curator_id"] == null)
                {
                    customerCreateViewModel.cu_curator_id = null;
                }
                else
                {
                    customerCreateViewModel.cu_curator_id = Convert.ToInt32(streamProvider.FormData["cu_curator_id"]);
                }
                if (streamProvider.FormData["cu_age"] == null)
                {
                    customerCreateViewModel.cu_age = null;
                }
                else
                {
                    customerCreateViewModel.cu_age = Convert.ToInt32(streamProvider.FormData["cu_age"]);
                }
                if (streamProvider.FormData["cu_status"] == null)
                {
                    customerCreateViewModel.cu_status = null;
                }
                else
                {
                    customerCreateViewModel.cu_status = Convert.ToByte(streamProvider.FormData["cu_status"]);
                }
                new BaseController();
                var current_id = BaseController.current_id;
                customerCreateViewModel.staff_id       = Convert.ToInt32(current_id);
                customerCreateViewModel.cu_create_date = DateTime.Now;
                var cu = _customerservice.GetLast();
                customerCreateViewModel.cu_code = Utilis.CreateCode("CU", cu.cu_id, 7);
                // mapping view model to entity
                var createdcustomer = _mapper.Map <customer>(customerCreateViewModel);
                createdcustomer.cu_thumbnail = fileName;
                // save new customer
                _customerservice.Create(createdcustomer);

                var get_last_id = _customerservice.GetLast();
                //Create address
                AddressCreateViewModel addressCreateViewModel = new AddressCreateViewModel
                {
                    add_province = Convert.ToString(streamProvider.FormData["add_province"]),

                    add_district = Convert.ToString(streamProvider.FormData["add_district"]),
                    add_ward     = Convert.ToString(streamProvider.FormData["add_ward"]),
                };
                addressCreateViewModel.customer_id = get_last_id.cu_id;
                var createAddress = _mapper.Map <address>(addressCreateViewModel);
                _addressservice.Create(createAddress);

                // return response
                response.Code    = HttpCode.OK;
                response.Message = MessageResponse.SUCCESS;
                response.Data    = createdcustomer;
                return(Ok(response));
            }
            catch (Exception ex)
            {
                response.Code    = HttpCode.INTERNAL_SERVER_ERROR;
                response.Message = ex.Message;
                response.Data    = null;
                Console.WriteLine(ex.ToString());

                return(Ok(response));
            }
        }