個人網(wǎng)站建站日記-面試寶典功能
目前,關(guān)于java的面試相關(guān)的,網(wǎng)上可以說是多的數(shù)不勝數(shù),但是關(guān)于.net的,找來找去卻發(fā)現(xiàn)很少,并且大部分相似,所以,我這里便想做個關(guān)于.net面試相關(guān)的功能,所以就我花了好幾個周末的時間,毛毛糙糙的算是把這個功能趕了出來,當(dāng)然也有很多不完善的地方。歡迎大家賞臉哈!現(xiàn)在我把我的實現(xiàn)步驟貼出來分享一下。
因為一個人開發(fā),個人精力以及能力有限,很多地方也會有不好的地方,所以希望大家多多包涵,如果感興趣的話,可以點進(jìn)去瞧瞧 https://www.xiandanplay.com/interview-manual
1.功能需求
大致羅列下需求:
- 題目分類管理
- 題目的發(fā)布,只要誰登錄了網(wǎng)站都可以發(fā)布,默認(rèn)是待審核的,網(wǎng)頁只顯示 已審核、待審核的功能
- 題目解析,發(fā)布者必須填寫自己的對題目的解析,其他人也可以提交參考解析,并且展現(xiàn)出來
- 對題目解析的查看,如果第一次查看這個題目,需要點擊“查看解析”的按鈕,如果已登錄并且查看過,則第二次就不需要點“查看解析”的按鈕了。
- 支持對題目的標(biāo)題、難度、標(biāo)簽的查詢
2.表(實體)結(jié)構(gòu)設(shè)計
這里我就用實體來表示了,因為我是用的ef,基本上用CodeFirst就是表結(jié)構(gòu)了
1.題目表
public class Question : Entity<long>
{
/// <summary>
/// 標(biāo)題
/// </summary>
public string Title { get; set; }
/// <summary>
/// 類型
/// </summary>
public QuestionTypeEnum QuestionType { get; set; }
/// <summary>
/// 提交人id
/// </summary>
public long UserId { get; set; }
/// <summary>
/// 提交人
/// </summary>
public User User { get; set; }
/// <summary>
/// 題目分類id
/// </summary>
public long? QuestionCategoryId { get; set; }
/// <summary>
/// 題目標(biāo)簽
/// </summary>
public string QuestionTag { get; set; }
/// <summary>
/// 瀏覽數(shù)
/// </summary>
public int BrowserCount { get; set; }
/// <summary>
/// 評論數(shù)
/// </summary>
public int CommentCount { get; set; }
/// <summary>
/// 收藏數(shù)
/// </summary>
public int CollectCount { get; set; }
/// <summary>
/// 獎勵金幣
/// </summary>
public int RewardCoin { get; set; }
/// <summary>
/// 是否發(fā)布
/// </summary>
public bool IsPublish { get; set; }
/// <summary>
/// 審核狀態(tài)
/// </summary>
public ApproveStatus ApproveStatus { get; set; }
}
2.題目解析表
public class QuestionAnalysis : Entity<long>
{
/// <summary>
/// 題目id
/// </summary>
public long InterviewQuestionId { get; set; }
/// <summary>
/// 解析簡要
/// </summary>
public string AnalysisContentDescription { get; set; }
/// <summary>
/// 題目解析
/// </summary>
public string AnalysisContent { get; set; }
/// <summary>
/// 提交人id
/// </summary>
public long UserId { get; set; }
/// <summary>
/// 提交人
/// </summary>
public User User { get; set; }
/// <summary>
/// 評論數(shù)
/// </summary>
public int CommentCount { get; set; }
/// <summary>
/// 贊同數(shù)
/// </summary>
public int AgreeCount { get; set; }
/// <summary>
/// 反對數(shù)
/// </summary>
public int AgainstCount { get; set; }
/// <summary>
/// 是否最佳解析
/// </summary>
public bool? Best { get; set; }
/// <summary>
/// 是否默認(rèn)
/// </summary>
public bool? IsDefault { get; set; }
/// <summary>
/// 收貨金幣
/// </summary>
public int? GetCoin { get; set; }
/// <summary>
/// 審核狀態(tài)
/// </summary>
public ApproveStatus ApproveStatus { get; set; }
}
3.題目分類表
public class InterviewQuestionCategory: Entity<long>
{
/// <summary>
/// 分類名稱
/// </summary>
public string Name { get; set; }
/// <summary>
/// 圖標(biāo)
/// </summary>
public string Icon { get; set; }
/// <summary>
/// 父分類id
/// </summary>
public long? ParentCategoryId { get; set; }
/// <summary>
/// 是否啟用
/// </summary>
public bool IsEnable { get; set; }
/// <summary>
/// 排序
/// </summary>
public int SortNo { get; set; }
/// <summary>
/// 描述
/// </summary>
public string Description { get; set; }
}
大致就羅列這幾個主要的吧。
4、業(yè)務(wù)邏輯實現(xiàn)
業(yè)務(wù)代碼邏輯倒是沒有特別復(fù)雜,都是寫CRUD,一看就會,這里舉例部分代碼。
遞歸獲取分類樹
public async Task<List<TreeVM>> GetCategoryTreesAsync()
{
List<InterviewQuestionCategory> categorys = await GetCategorys();
return BuildTrees(categorys);
}
private List<TreeVM> BuildTrees(List<InterviewQuestionCategory> interviewQuestionCategories)
{
List<TreeVM> list = new List<TreeVM>();
var roots = interviewQuestionCategories.Where(s => !s.ParentCategoryId.HasValue).OrderBy(s => s.SortNo);
foreach (var root in roots)
{
TreeVM interviewCategoryTreeVM = new TreeVM();
interviewCategoryTreeVM.Id = root.Id;
interviewCategoryTreeVM.Label = root.Name;
interviewCategoryTreeVM.Icon = root.Icon;
GetChildrens(root, interviewQuestionCategories, interviewCategoryTreeVM);
list.Add(interviewCategoryTreeVM);
}
return list;
}
private List<TreeVM> GetChildrens(InterviewQuestionCategory root, List<InterviewQuestionCategory> interviewQuestionCategories, TreeVM interviewCategoryTreeVM = null)
{
var childrens = interviewQuestionCategories.Where(s => s.ParentCategoryId == root.Id).OrderBy(s => s.SortNo);
List<TreeVM> list = new List<TreeVM>();
foreach (var item in childrens)
{
TreeVM treeVM = new TreeVM();
treeVM.Id = item.Id;
treeVM.Label = item.Name;
treeVM.Icon = item.Icon;
list.Add(treeVM);
if (interviewQuestionCategories.Any(s => s.ParentCategoryId == item.Id))
GetChildrens(item, interviewQuestionCategories, treeVM);
}
interviewCategoryTreeVM.Children = list;
return list;
}
創(chuàng)建題目
public async Task<long> SaveQuestion(SaveInterviewQuestionVM saveInterviewQuestion)
{
if (saveInterviewQuestion.IsPublish && string.IsNullOrEmpty(saveInterviewQuestion.AnalysisContent))
throw new ValidationException("題目解析內(nèi)容為空");
var questionRepository = unitOfWork.GetRepository<Question>();
var questionAnalysisRepository = unitOfWork.GetRepository<QuestionAnalysis>();
Question question = null;
QuestionAnalysis questionAnalysis = null;
bool isAddQuestion = true;
bool isAddAnalysis = true;
if (saveInterviewQuestion.Id.HasValue)
{
question = await questionRepository.SelectByIdAsync(saveInterviewQuestion.Id.Value);
if (question.Id != CurrentLoginUser.UserId)
throw new ValidationException("無權(quán)修改");
questionAnalysis = await questionAnalysisRepository.Select(s => s.InterviewQuestionId == saveInterviewQuestion.Id.Value && s.IsDefault == true).FirstOrDefaultAsync();
isAddQuestion = false;
}
else
{
question = new Question()
{
Id = CreateEntityId()
};
}
question.Title = saveInterviewQuestion.Title;
question.QuestionDifficulty = saveInterviewQuestion.Difficultion;
question.QuestionCategoryId = saveInterviewQuestion.CategoryId;
question.UserId = LoginUserId.Value;
question.ApproveStatus = ApproveStatus.ToPass;
question.IsPublish = saveInterviewQuestion.IsPublish;
question.QuestionType = QuestionTypeEnum.面試題;
if (saveInterviewQuestion.QuestionTags != null && saveInterviewQuestion.QuestionTags.Length > 0)
question.QuestionTag = string.Join(',', saveInterviewQuestion.QuestionTags);
if (questionAnalysis == null)
{
questionAnalysis = new QuestionAnalysis();
questionAnalysis.Id = CreateEntityId();
questionAnalysis.IsDefault = true;
questionAnalysis.InterviewQuestionId = question.Id;
questionAnalysis.UserId = LoginUserId.Value;
questionAnalysis.ApproveStatus = ApproveStatus.ToPass;
questionAnalysis.AnalysisContent = saveInterviewQuestion.AnalysisContent;
}
else
{
questionAnalysis.AnalysisContent = saveInterviewQuestion.AnalysisContent;
isAddAnalysis = false;
}
if (isAddQuestion)
await questionRepository.InsertAsync(question);
else
await questionRepository.UpdateAsync(question);
if (isAddAnalysis)
await questionAnalysisRepository.InsertAsync(questionAnalysis);
else
await questionAnalysisRepository.UpdateAsync(questionAnalysis);
await unitOfWork.CommitAsync();
return question.Id;
}
使用Redis的Set類型處理贊成與反對操作
public async Task<bool> AgreeFunc(InterviewQuestionAnalysisAgreeVM agreeVM)
{
var cache = CacheClient.CreateClient();
string cacheKey = $"{CacheKey.InterviewQuestionAnalysisAgreeKey}_{agreeVM.InterviewQuestionAnalysisId}";
string cacheValue = $"{agreeVM.AgreeType}_{agreeVM.InterviewQuestionAnalysisId}_{LoginUserId.Value}";
if (agreeVM.AgreeType == 1)
{
string oldCacheValue = $"2_{agreeVM.InterviewQuestionAnalysisId}_{LoginUserId.Value}";
cache.SetRemove(cacheKey, oldCacheValue);
}
else
{
string oldCacheValue = $"1_{agreeVM.InterviewQuestionAnalysisId}_{LoginUserId.Value}";
cache.SetRemove(cacheKey, oldCacheValue);
}
bool result = cache.AddSet(cacheKey, cacheValue);
if (result)
{
List<string> setMembers= cache.GetMembersBySetKey(cacheKey);
int agreeCount = 0;
int againstCount = 0;
foreach (var item in setMembers)
{
if (item.StartsWith('1'))
agreeCount += 1;
else
againstCount += 1;
}
await qaRepository.UpdateAgree(agreeVM.InterviewQuestionAnalysisId, agreeCount, againstCount);
await unitOfWork.CommitAsync();
return true;
}
return false;
}
以上只是部分代碼,僅供一下參考,但是也花費了我好多個周末的時間,其它的就不說了,放幾個截圖看看吧,部分的功能還沒完善,這個只是初版。
面試寶典首頁圖(每日推薦那里還沒完成)

詳細(xì)的面試列表界面

查看頁面

提交參考解析的頁面

作者:程序員奶牛
個人主頁:https://www.xiandanplay.com/user/user-home?id=16782377660907520

浙公網(wǎng)安備 33010602011771號