예제 #1
0
 public WorkHistoryViewModel()
 {
     if (_workHistory == null)
     {
         _workHistory = new WorkHistoryInfo();
     }
 }
        /// <summary>
        /// Update WorkHistory
        /// </summary>
        /// <param name="entityToUpdate"></param>
        /// <returns></returns>
        public async Task <int> UpdateAsync(WorkHistoryInfo entity)
        {
            using (var conn = OpenDBConnection())
            {
                StringBuilder sql = new StringBuilder();
                sql.Length = 0;

                sql.AppendLine(" UPDATE dbo.WorkHistory                           ");
                sql.AppendLine("    SET Position   = @Position,                   ");
                sql.AppendLine("        CompanyName  = @CompanyName,              ");
                sql.AppendLine("        OrderIndex      = @OrderIndex,            ");
                sql.AppendLine("        StartDate      = @StartDate,              ");
                sql.AppendLine("        EndDate    = @EndDate,                    ");
                sql.AppendLine("        PersonId    = @PersonId,                  ");
                sql.AppendLine("        UpdatedAt    = @UpdatedAt,                ");
                sql.AppendLine("        UpdatedBy    = @UpdatedBy                 ");
                sql.AppendLine(" WHERE Id = @Id  AND Status = 1                   ");

                var param = new
                {
                    id          = entity.Id,
                    position    = entity.Position,
                    companyName = entity.CompanyName,
                    orderIndex  = entity.OrderIndex,
                    startDate   = entity.StartDate,
                    endDate     = entity.EndDate,
                    personId    = entity.PersonId,
                    updatedAt   = entity.UpdatedAt,
                    updatedBy   = entity.UpdatedBy
                };
                return(await conn.ExecuteAsync(sql.ToString(), param));
            }
        }
        /// <summary>
        /// Create WorkHistory
        /// </summary>
        /// <param name="workHistoryObj"></param>
        /// <returns></returns>
        public async Task <WorkHistoryViewModel> CreateWorkHistory(CreateWorkHistoryResource workHistoryObj)
        {
            model.AppResult.Result = false;
            // Validate Start/End Date
            if (!Functions.ValidateInputTime(workHistoryObj.StartDate, workHistoryObj.EndDate))
            {
                model.AppResult.Message = Constant.DATETIME_ERROR;
                return(model);
            }
            //Validate CompanyName
            if (String.IsNullOrEmpty(workHistoryObj.CompanyName) || workHistoryObj.CompanyName.Length > 255)
            {
                model.AppResult.Message = Constant.COMPANY_ERROR;
                return(model);
            }
            //Validate Position
            if (String.IsNullOrEmpty(workHistoryObj.Position) || workHistoryObj.Position.Length > 255)
            {
                model.AppResult.Message = Constant.POSITION_ERROR;
                return(model);
            }
            // Validate PersonId
            var taskValidPeronId = await _workHistoryRepository.ValidatePersonId(workHistoryObj.PersonId);

            if (taskValidPeronId < 1)
            {
                model.AppResult.Message = Constant.PERSONID_ERROR;
                return(model);
            }
            // Define DateTime.Year = "1111" is null
            var valueStartDate      = Functions.ConvertDateTime(workHistoryObj.StartDate);
            var valueEndDate        = Functions.ConvertDateTime(workHistoryObj.EndDate);
            var tempWorkHistoryInfo = new WorkHistoryInfo()
            {
                Position    = workHistoryObj.Position,
                CompanyName = workHistoryObj.CompanyName,
                StartDate   = valueStartDate,
                EndDate     = (valueEndDate.Year == 1111) ? (DateTime?)null : valueEndDate, // Define DateTime.Year = "1111" is null
                PersonId    = workHistoryObj.PersonId,
                UpdatedBy   = null,
                UpdatedAt   = null,
                CreatedBy   = Helpers.HttpContext.CurrentUser,
                CreatedAt   = DateTime.Now
            };
            var id = await _workHistoryRepository.InsertAsync(tempWorkHistoryInfo);

            var result = await GetWorkHistoryById(id);

            model.AppResult.DataResult = result;
            model.AppResult.Result     = true;
            model.AppResult.Message    = Constant.INSERT_SUCCESS;
            return(model);
        }
        /// <summary>
        /// Create WorkHistory
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task <int> InsertAsync(WorkHistoryInfo entity)
        {
            var orderIndex = 0;

            if (CountWorkHistory(entity.PersonId) <= 0)
            {
                orderIndex = 1;
            }
            else
            {
                orderIndex = GetMaxOrderIndex(entity.PersonId) + 1;
            }

            using (var conn = OpenDBConnection())
            {
                StringBuilder sql = new StringBuilder();
                sql.Length = 0;

                sql.AppendLine(" INSERT INTO                             ");
                sql.AppendLine("      dbo.WorkHistory (Position,         ");
                sql.AppendLine("                   CompanyName,          ");
                sql.AppendLine("                   OrderIndex,           ");
                sql.AppendLine("                   StartDate,            ");
                sql.AppendLine("                   EndDate,              ");
                sql.AppendLine("                   PersonId,             ");
                sql.AppendLine("                   CreatedAt,            ");
                sql.AppendLine("                   CreatedBy)            ");
                sql.AppendLine(" VALUES (@Position,                      ");
                sql.AppendLine("         @CompanyName,                   ");
                sql.AppendLine("         @OrderIndex,                    ");
                sql.AppendLine("         @StartDate,                     ");
                sql.AppendLine("         @EndDate,                       ");
                sql.AppendLine("         @PersonId,                      ");
                sql.AppendLine("         @CreatedAt,                     ");
                sql.AppendLine("         @CreatedBy)                     ");
                sql.AppendLine("SELECT SCOPE_IDENTITY()                  ");

                var param = new
                {
                    position    = entity.Position,
                    companyName = entity.CompanyName,
                    orderIndex  = orderIndex,
                    startDate   = entity.StartDate,
                    endDate     = entity.EndDate,
                    personId    = entity.PersonId,
                    createdAt   = entity.CreatedAt,
                    createdBy   = entity.CreatedBy
                };
                return(await conn.ExecuteScalarAsync <int>(sql.ToString(), param));
            }
        }