Chloe Finn
Chloe Finn

Reputation: 257

Adding someone to an Event which is a many to many

Have an appointments/ceremony app where I would like Volunteers to be able to sign up for a ceremony. They must be signed in to do so.

A ceremony can have many Volunteers and a Volunteer can have many ceremonies

I'm trying to do this while in FullCalendar. I want the Volunteer to click on a ceremony and click a button to join the event.

I'm having major trouble understanding how I can add the Volunteer to the Ceremony. I know I need to add to the list within the Ceremony entity.

Here's my controller that I'm working on:

    [HttpPost]
    public JsonResult AddVolunteerToCeremony(Volunteer v, Appointments a)
    {
        string username = Membership.GetUser().UserName;

        var getVolunteer = (from vol in db.Volunteers
                            where username == vol.Username
                            select vol).SingleOrDefault();

        v.Appointments = new List<Appointments>();

        var status = false;
        using (ChurchDBContext db = new ChurchDBContext())
        {
            if (a.AppointmentId > 0)
            {
                //Update the event
                var getCeremonies = db.Appointments.Where(d => d.AppointmentId == a.AppointmentId).FirstOrDefault();
                if (v != null)
                {
                    v.Appointments.Add(getVolunteer);

                }
            }

            db.SaveChanges();
            status = true;
        }
        return new JsonResult { Data = new { status = status } };
    }

Entities:

public class Appointments
{
    [Key]
    public int AppointmentId { get; set; }

    public string DetailsOfAppointment { get; set; }

    public int? Fee { get; set; }

    public string RoomType { get; set; }

    public string NameOfApplicant { get; set; }

    public string ApplicantPhoneNumber { get; set; }

    public string ApplicantEmail { get; set; }

    [DataType(DataType.DateTime)]
    [DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:dd/MM/yyyy HH:mm}")]
    public DateTime DateOfAppointment { get; set; }

    public string ThemeColour { get; set; }

    public Boolean Confirmed { get; set; }

    [ForeignKey("Admins")]
    public int AdministrationId { get; set; }

    [ForeignKey("Church")]
    public int ChurchId { get; set; }

    //[ForeignKey("Volunteers")]
    //public int VolunteerId { get; set; }

    public virtual Church Church { get; set; }

    public virtual Administration Admins { get; set; }

    public ICollection <Volunteer> Volunteers { get; set; }

    public Appointments()
    {
        this.Volunteers = new HashSet<Volunteer>();
    }
}

 public class Volunteer
{
    [Key]
    public int VolunteerId { get; set; }

    public string Name { get; set; }

    public string Username { get; set; }

    public string Email { get; set; }

    public bool GardaVetted { get; set; }

    public string VolunteerRole { get; set; }

    public string VolunteerPhoneNumber { get; set; }

    [ForeignKey("Church")]
    public int ChurchId { get; set; }

    //Foreign Key
    public virtual Church Church { get; set; }

    public virtual ICollection<Appointments> Appointments { get; set; }

    //public virtual Appointments app { get; set; }

    public Volunteer()
    {
        this.Appointments = new HashSet<Appointments>();
    }

}

Am I onto it or completely off?

Upvotes: 3

Views: 32

Answers (1)

wp78de
wp78de

Reputation: 18980

It looks like you just update the UI data but not the DB.

...
        if (a.AppointmentId > 0)
        {
            //Update the event
            var getCeremonies = db.Appointments.Where(d => d.AppointmentId == a.AppointmentId).FirstOrDefault();
            if (v != null && getCeremonies != null)
            {
                v.Appointments.Add(getVolunteer);
                getCeremonies.Volunteers.Add(getVolunteer);
            }
        }

        db.SaveChanges();

Upvotes: 1

Related Questions