public ActionResult <DTOAppointmentPrivate> GetAppointment(Guid appointmentId) { using (var unit = _factory.GetUOF()) { var dbAppointment = unit.Appointments.GetEager(appointmentId); if (dbAppointment == null) { return(BadRequest(new { message = $"Appointment with id '{appointmentId}' did not exist" })); } DTOAppointmentPrivate dtoAppointment = new DTOAppointmentPrivate { Participants = new List <DTOSimpleUser>() }; Mapper.Map(dbAppointment, dtoAppointment); var claims = User.Claims; var userId = claims.FirstOrDefault(x => x.Type == "id")?.Value; if (userId != dbAppointment.OwnerId.ToString()) { return(Unauthorized()); } foreach (var participant in dbAppointment.Participants) { var dtoUser = new DTOSimpleUser(); Mapper.Map(participant.Calendar.User, dtoUser); dtoAppointment.Participants.Add(dtoUser); } return(dtoAppointment); } }
public ActionResult <DTOCalendarPrivate> GetCalendarUser(Guid id) { var claims = User.Claims; var userId = claims.FirstOrDefault(x => x.Type == "id")?.Value; if (userId != id.ToString()) { return(Unauthorized()); } using (var unit = _factory.GetUOF()) { try { var dbCalendar = unit.Calendars.GetEagerByUserId(id); if (dbCalendar == null) { return(BadRequest(new { message = $"No calendar was found with id '{id}'" })); } var dtoCalendar = new DTOCalendarPrivate { Appointments = new List <DTOAppointmentPrivate>() }; Mapper.Map(dbCalendar, dtoCalendar); foreach (var appointment in dbCalendar.Appointments) { var dtoAppointment = new DTOAppointmentPrivate { Participants = new List <DTOSimpleUser>() }; Mapper.Map(appointment.Appointment, dtoAppointment); foreach (var participant in appointment.Appointment.Participants) { var user = participant.Calendar.User; var dtoUser = new DTOSimpleUser(); Mapper.Map(user, dtoUser); dtoAppointment.Participants.Add(dtoUser); } dtoCalendar.Appointments.Add(dtoAppointment); } return(dtoCalendar); } catch (Exception e) { Console.WriteLine(e); throw; } } }
public ActionResult <Appointment> AddAppointment([FromBody] DTOAppointment dtoAppointment) { var claims = User.Claims; var userId = claims.FirstOrDefault(x => x.Type == "id")?.Value; if (userId != dtoAppointment.OwnerId.ToString()) { return(Unauthorized()); } using (var unit = _factory.GetUOF()) { try { var dbUser = unit.Users.GetEager(dtoAppointment.OwnerId); var startTime = StringToDateTime.Convert(dtoAppointment.StartTime); var endTime = StringToDateTime.Convert(dtoAppointment.EndTime); // Create appointment var appointment = new Appointment { Participants = new List <CalendarAppointment>(), StartTime = startTime, EndTime = endTime, Text = dtoAppointment.Text, Title = dtoAppointment.Title, MaxParticipants = dtoAppointment.MaxParticipants, OwnerId = dtoAppointment.OwnerId }; //Check if there is a spot in the calendar var occupiedTime = dbUser.Calendar.Appointments.Any(x => x.Appointment.StartTime <= appointment.StartTime && x.Appointment.EndTime >= appointment.StartTime); if (occupiedTime) { return(BadRequest(new { message = "Please pick a free spot in the calendar" })); } var calendarAppointment = new CalendarAppointment { Appointment = appointment, Calendar = dbUser.Calendar }; dbUser.Calendar.Appointments.Add(calendarAppointment); unit.Complete(); var res = new DTOAppointmentPrivate { Participants = new List <DTOSimpleUser>() }; var simpleUser = new DTOSimpleUser(); Mapper.Map(dbUser, simpleUser); res.Participants.Add(simpleUser); Mapper.Map(appointment, res); return(CreatedAtAction("GetAppointment", new { appointmentId = res.Id }, res)); } catch (Exception e) { Console.WriteLine(e); throw; } } }
public ActionResult <DTOAppointmentPrivate> AddUserToAppointment([FromBody] DTOId userId, Guid appointmentId) { using (var unit = _factory.GetUOF()) { try { var dbAppointment = unit.Appointments.GetEager(appointmentId); if (dbAppointment == null) { return(BadRequest(new { message = $"Appointment with id '{appointmentId}' did not exist" })); } var dbUser = unit.Users.GetEager(userId.Id); if (dbUser == null) { return(BadRequest(new { message = $"User with id '{userId.Id}' did not exist" })); } if (dbAppointment.MaxParticipants <= dbAppointment.Participants.Count) { return(BadRequest(new { message = "Appointment is full" })); } if (dbAppointment.Participants.Any(x => x.Calendar.User.NormalizedEmail == dbUser.NormalizedEmail)) { return(BadRequest(new { message = "User is already in the appointment" })); } var calendarAppointment = new CalendarAppointment { Appointment = dbAppointment, Calendar = dbUser.Calendar }; dbAppointment.Participants.Add(calendarAppointment); unit.Complete(); var res = new DTOAppointmentPrivate { Participants = new List <DTOSimpleUser>() }; foreach (var participant in dbAppointment.Participants) { var simpleUser = new DTOSimpleUser(); Mapper.Map(participant.Calendar.User, simpleUser); res.Participants.Add(simpleUser); } Mapper.Map(dbAppointment, res); return(CreatedAtAction("GetAppointment", new { appointmentId = res.Id }, res)); } catch (Exception e) { Console.WriteLine(e); throw; } } }