I am trying to create a random number generator, if the random number exists in the database loop through the block of code again until it finds a number that does not exist. Save value to database. Any help on this would be great thank you.
public IHttpActionResult BookMatch(int id)
{
Guid guid = Guid.NewGuid();
var user = User.Identity.GetUserId();
var getuser = db.AspNetUsers.Find(user);
var match = db.Matches.Find(id);
Random rnd = new Random();
var peg = rnd.Next(match.PegRangeMin, match.PegRangeMax);
var alreadyExists = db.Pegs.Any(x => x.MatchId == match.Id && x.PegNo == peg);
if (alreadyExists == true)
{
//do somthing
}
var result = new Booked()
{
MatchId = id,
BookerId = user,
TicketNo = guid.ToString(),
//pegNo = randompeg
};
db.Bookeds.Add(result);
db.SaveChanges();
if (result == null)
{
throw new Exception();
}
var getmatch = db.Matches.Find(id);
var body = "<p>Email From: {0} ({1}) </p>" +
"<h3>Match Booking Confirmation:</h3>" +
"<b>Ticket Number:</b><p> {2} </p>" +
"<b>Venue:</b><p> {3} </p>" +
"<b>date and time:</b><p>{4}</p>";
var message = new MailMessage();
message.To.Add(new MailAddress(getuser.Email));
message.From = new MailAddress("nicholas.mciver@Activeplan.co.uk");
message.Subject = "Matchbooker Confirmation Email";
message.Body = string.Format(body, getuser.GetFullName(), getuser.Email, result.TicketNo, getmatch.Fishery.Name, getmatch.DateTime);
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
smtp.Send(message);
}
return Ok();
}
Aucun commentaire:
Enregistrer un commentaire