예제 #1
0
        /// <summary>
        /// Check condition for upload image
        /// </summary>
        /// <param name="path"></param>
        /// <param name="objectFile"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <IEnumerable> UploadImage(string path, FileUpload objectFile, int id)
        {
            if (await _personRepository.CheckPersonExisting(id) <= 0)
            {
                return(null);
            }
            else
            {
                string StaffId = string.Empty;
                var    getdate = DateTime.Now.ToString("yyyyMMdd");
                DateTime.Now.ToString();
                try
                {
                    if (Functions.HasImageExtension(Path.GetExtension(objectFile.files.FileName)) == false)
                    {
                        return(null);
                    }
                    else
                    {
                        if (objectFile.files.Length > 0)
                        {
                            if (!Directory.Exists(path))
                            {
                                Directory.CreateDirectory(path);
                            }
                            StaffId = string.Format($"{getdate + id}");
                            string fileName = StaffId + Path.GetExtension(objectFile.files.FileName);
                            using (FileStream fileStream = System.IO.File.Create(path + fileName))
                            {
                                objectFile.files.CopyTo(fileStream);
                                fileStream.Flush();
                                await _uploadRepository.UploadImage(fileName, path);

                                return(string.Concat("~\\Avatar\\", fileName));
                            }
                        }
                        else
                        {
                            return("Not any image Uploaded!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    return(ex.Message);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// VALIDATE INFO PERSON AND AVATAR BEFORE INSERT TO DATABASE
        /// </summary>
        /// <param name="person"></param>
        /// <param name="objectFile"></param>
        /// <returns></returns>
        public async Task <PersonViewModel> InsertPerson(Person person, FileUpload objectFile)
        {
            model.PersonInfo           = person;
            model.PersonInfo.CreatedBy = WebAPI.Helpers.HttpContext.CurrentUser;
            var genderValue    = Convert.ToInt32(model.PersonInfo.Gender);
            var getdate        = DateTime.Now.ToString("yyyyMMdd");
            int newestIdPerson = 1;

            DateTime.Now.ToString();
            if (person.FullName == "" || person.FullName == null || person.FullName.GetType() != typeof(string) || person.FullName.Length > 256)
            {
                model.AppResult.Message = Constant.NAME_ERROR;
                return(model);
            }
            if (Functions.CheckLocation(Convert.ToInt32(model.PersonInfo.Location)) != true || model.PersonInfo.Location.ToString() == null)
            {
                model.AppResult.Message = Constant.LOCATION_INVALID;
                return(model);
            }
            else if (Functions.IsPhoneNumber(person.Phone) == false || person.Phone == null || await _personRepository.CheckPhoneExisting(person.Phone) > 0)
            {
                model.AppResult.Message = Constant.PHONE_ERROR;
                return(model);
            }
            else if (genderValue < 0 || genderValue > 2)
            {
                model.AppResult.Message = Constant.GENDER_INVALID;
                return(model);
            }
            else if (Functions.HandlingEmail(person.Email) == false || await _personRepository.CheckEmailExist(person.Email) > 0)
            {
                model.AppResult.Message = Constant.EMAIL_ERROR;
                return(model);
            }
            #region Validate phone

            /* else if (Functions.IsPhoneNumber(person.Phone) == false)
             * {
             *   model.AppResult = new AppResult { Result = false, StatusCd = "400", Message = "Phone invalid.!", DataResult = null };
             *   return model;
             * }*/
            #endregion

            else if (!Functions.ValidateYearOfBirth(person.YearOfBirth))
            {
                model.AppResult.Message = Constant.YEAROFBIRTH_INVALID;
                return(model);
            }
            else
            {
                if (await _personRepository.AmountOfPerson() > 0)
                {
                    newestIdPerson = await _personRepository.GetMaxIdPerson() + 1;

                    person.StaffId = string.Format($"{getdate + newestIdPerson}");
                }
                else
                {
                    person.StaffId = string.Format($"{getdate + newestIdPerson}");
                }
                person.Avatar           = "";
                model.PersonInfo.Status = true;
                int id = await _personRepository.InsertAsync(model.PersonInfo);

                model.PersonInfo.Id = newestIdPerson;
                //Insert image
                //get path \\Avatar\\ from appseting.json

                var ImagePath = _appSettings.ImagePath.ToString();

                //return domain
                var host = WebAPI.Helpers.HttpContext.Current.Request.Host.Value;



                string path = _webHostEnvironment.WebRootPath + ImagePath;
                DateTime.Now.ToString();
                if (objectFile.files == null)
                {
                    string imageName = "avatar-default.png";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    await _uploadRepository.UploadImage(imageName, path);

                    model.PersonInfo.Avatar    = imageName;
                    model.AppResult.Message    = Constant.CREATE_PERSON_SUCCESS;
                    model.AppResult.DataResult = model.PersonInfo;
                    return(model);
                }
                if (Functions.HasImageExtension(Path.GetExtension(objectFile.files.FileName.ToLower())) == false)
                {
                    model.AppResult.Message = Constant.IMAGE_INVALID;
                    return(model);
                }
                else
                {
                    if (objectFile.files.Length > 0)
                    {
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        string fileName = person.StaffId + Path.GetExtension(objectFile.files.FileName.ToLower());
                        using (FileStream fileStream = System.IO.File.Create(path + fileName))
                        {
                            objectFile.files.CopyTo(fileStream);
                            fileStream.Flush();
                            await _uploadRepository.UploadImage(fileName, path);

                            model.PersonInfo.Avatar = fileName;
                            //return http request and infor person created

                            model.AppResult.Message    = Constant.CREATE_PERSON_SUCCESS;
                            model.AppResult.DataResult = model.PersonInfo;
                            return(model);
                        }
                    }
                    else
                    {
                        //return http error request
                        model.AppResult.Message    = "Create person Failed!";
                        model.AppResult.DataResult = model.PersonInfo;
                        return(model);
                    }
                }
            }
        }
예제 #3
0
 public MessageResponse UploadImage(IFormFile image)
 {
     return(_uploadRepository.UploadImage(image));
 }