/// <summary> /// return 1(success),return -1(error-insert),return -2(error-age), /// return -3(club have enough foreign footballer) /// </summary> /// <param name="entity"></param> /// <returns></returns> public int Update(footballer entity) { try { // check age in accordance with the regulations var info_rule = db.general_rule.FirstOrDefault(); var age_footballer = DateTime.Now.Year - entity.birth_date.Value.Year; var num_foreignFootballer_in_club = new football_club_dao().CountAllForeignFootballerInclub(entity.footballClub_id); if (age_footballer < info_rule.min_age || age_footballer > info_rule.max_footballer) { return(-2); } else if (entity.footballer_type_id == "FOREIGN" && num_foreignFootballer_in_club >= info_rule.max_foreign_footballer) { return(-3); } var info = db.footballers.Find(entity.id); info.name = entity.name; info.birth_date = entity.birth_date; info.hometown = entity.hometown; info.position = entity.position; info.footballer_type_id = entity.footballer_type_id; info.footballClub_id = entity.footballClub_id; db.SaveChanges(); return(1); } catch (Exception) { return(-1); } }
/// <summary> /// Get all footballer by Club -> Season -> All /// Tham biến seasonID /// </summary> /// <param name="seasonID"></param> /// <param name="page"></param> /// <param name="page_size"></param> /// <param name="club_id"></param> /// <returns></returns> public IEnumerable <footballer> ListAllPaging(ref string seasonID, int page, int page_size, string club_id = null) { var result_club = new football_club_dao().CheckFootballClub(club_id); var result_season = new season_dao().CheckSeason(seasonID); var tmp_seasonID = seasonID; //Duyệt theo thứ tự ưu tiên club -> season if (result_club) { var model = from a in db.footballers where a.footballClub_id == club_id select a; seasonID = null; return(model.OrderBy(x => x.id).ToPagedList(page, page_size)); } else if (result_season) { var model = (from fl in db.footballers from m in db.matches from mg in db.match_goal where (mg.match_id == m.id && m.season_id == tmp_seasonID && mg.footballer_id == fl.id) select fl).Distinct(); return(model.OrderBy(x => x.name).ToPagedList(page, page_size)); } else { seasonID = null; return(db.footballers.OrderBy(x => x.footballClub_id).ToPagedList(page, page_size)); } }
//common lib /// <summary> /// check match by rule, /// return errorMessage + /// return 1 (insert success) /// return -1 (insert falied) /// return -2 (club has played in the round ), /// return -3 (match has existed in the season ) /// </summary> /// <param name="entity"></param> /// <returns></returns> public int Insert(ref string errorMessage, match entity) { try { //get info club by homeClub,guestClub var club_dao = new football_club_dao(); var homeClub = club_dao.Get_By_Id(entity.home_club); var guestClub = club_dao.Get_By_Id(entity.guest_club); if (CheckNumAttendOfClub_InRound(entity.home_club, entity.round_id, entity.season_id) > 0) { errorMessage = "CLB " + homeClub.name + " đã thi đấu trong vòng đấu đang chọn"; return(-2); } if (CheckNumAttendOfClub_InRound(entity.guest_club, entity.round_id, entity.season_id) > 0) { errorMessage = "CLB " + guestClub.name + " đã thi đấu trong vòng đấu đang chọn"; return(-2); } if (CheckNumAttendOfMatch_InSeason(entity.home_club, entity.guest_club, entity.round_id, entity.season_id) > 0) { errorMessage = "Trận đấu " + homeClub.name + "-" + guestClub.name + " đã tồn tại trong mùa giải"; return(-3); } if (string.IsNullOrEmpty(entity.score)) { entity.score = "0"; } db.matches.Add(entity); db.SaveChanges(); new rank_dao().Update(entity.season_id); return(1); } catch (Exception) { return(-1); } }