public static Sms SmsDTOToSms(this SmsDTO smsDto) { var newSms = new Sms(); newSms.Sender = smsDto.Sender; newSms.Body = smsDto.Body; newSms.Received = DateTime.Now; return newSms; }
//TODO Error handling when sms data is not in correct format and correct length private string processSms(Sms sms) { try { string sender = sms.Sender; string msg = sms.Body; var data = msg.Split('#'); SmsType type = SmsType.Answer; string res = ""; switch (data[1]) { case "ac": type = SmsType.Admin; res = ProcessAdmin(data.Skip(2).ToArray()); break; case "ic": type = SmsType.Answer; if (data.Length > 4) res = AnswerPost(sender, data[2], data[3], data[4], sms); else res = "Fejl ved indsendelse af svar. Prøv igen"; break; case "ct": type = SmsType.CreateTeam; res = CreateTeam(sender, data.Skip(2).ToList()); break; case "ci": type = SmsType.Checkin; int postid; var parse_res = int.TryParse(data[2], out postid); if (!parse_res) { res = "Der skete en fejl"; } else { res = CheckInTeam(sender, postId: postid, raceId: data[3]); } break; case "sp": type = SmsType.Startpost; int postid2; var parse_res2 = int.TryParse(data[2], out postid2); if (!parse_res2) { res = "Der skete en fejl"; } else { res = StartPost(sender, postId: postid2, raceId: data[3]); } break; case "pt": type = SmsType.PointGiver; double points; var parse_res3 = double.TryParse(data[5], out points); if (!parse_res3) { res = "Der skete en fejl. Du har ikke udfyldt point."; } else { res = GivePoints(sender, raceid: data[2], teamid: data[3], pointValue: points, postid: data[4]); } break; } return res; } catch { return "Der skete en fejl. Fejlkode 0x0b22300a"; } }
private string AnswerPost(string sender, string raceId, string postId, string answerid, Sms sms) { var team = FindByNumber(sender, raceId); Race r = null; if (team != null) r = team.Race; else r = context.Races.Find(int.Parse(raceId)); var p = r.Posts.Where(q => q.Id == int.Parse(postId)).FirstOrDefault(); var pa = p.Answers.Where(k => k.Id == int.Parse(answerid)).FirstOrDefault(); var noOfTriesForTeam = context.Answers.Count(an => an.Team.Id == team.Id && an.Post.Id == p.Id); if ((r.NoOfTriesPerPost > 0 && noOfTriesForTeam >= r.NoOfTriesPerPost) || (noOfTriesForTeam >= p.Answers.Count)) { return "Posten er allerede besvaret"; } var a = new Answer { AnsweredAt = DateTime.Now, Team = team, ChosenAnswer = pa, Post = p, Sms = sms, CorrectAnswerChosen = pa.CorrectAnswer }; context.Answers.Add(a); context.SaveChanges(); var textToShow = (pa.CorrectAnswer ? p.CorrectAnswerText : p.WrongAnswerText); if (r.ShowNextPost && pa.NextPost != null) { textToShow += ". Næste post er " + pa.NextPost.Title; if (pa.NextPost.lattitude != 0 || pa.NextPost.longitude != 0) { textToShow += "Den er placereret på: "; textToShow += "(" + pa.NextPost.lattitude.ToString() + ", "; textToShow += pa.NextPost.longitude.ToString() + ")"; } } var checkinForTeam = context.Checkins.Where(c => c.TeamId == team.Id && c.PostId == p.Id).ToList(); checkinForTeam.ForEach(s => s.CheckOut = DateTime.Now); context.SaveChanges(); return textToShow; }