public async Task <ParentChildIdDTO> MapDesignationsType(ParentChildIdDTO parentChildIdDTO) { var updatedDate = Converters.GetCurrentEpochTime(); //delete all existing records var allExistingRecords = await this._dbContext.DesignationTypeMapping.Where(s => s.DesignationTypeID == parentChildIdDTO.ParentID).ToListAsync(); if (allExistingRecords.Any()) { allExistingRecords.ForEach(a => { a.IsDeleted = true; a.UpdatedDate = updatedDate; }); await this._dbContext.SaveChangesAsync(); } //add or update new records allExistingRecords = await this._dbContext.DesignationTypeMapping.Where(s => s.DesignationTypeID == parentChildIdDTO.ParentID).ToListAsync(); foreach (var item in parentChildIdDTO.ChildID) { var existingRecord = allExistingRecords.SingleOrDefault(s => s.DesignationTypeID == parentChildIdDTO.ParentID && s.DesignationMasterID == item); if (existingRecord == null) { //add records await this.SaveUpdateAsync(new DesignationTypeMappingDTO() { DesignationTypeID = parentChildIdDTO.ParentID, DesignationMasterID = item }); } else { //update existing records existingRecord.IsDeleted = false; existingRecord.UpdatedDate = updatedDate; this._dbContext.Entry(existingRecord).State = EntityState.Modified; await this._dbContext.SaveChangesAsync(); } } this.DisplayMessage = CommonMethods.GetMessage(this.logType, LogAction.Add); return(parentChildIdDTO); }
public async Task <object> MapDesignationsType([FromBody] ParentChildIdDTO model) { try { var data = await repository.MapDesignationsType(model); _response.Result = data; _response.IsSuccess = repository.IsSuccess; _response.ErrorMessages = repository.ErrorMessages; _response.DisplayMessage = repository.DisplayMessage; } catch (Exception ex) { _response.IsSuccess = false; _response.ErrorMessages = new List <ErrorMessageDTO>() { new ErrorMessageDTO() { Message = ex.ToString() } }; } return(_response); }