Esempio n. 1
0
        public ActionResult <List <DTOSimpleUser> > GetFavorites(Guid userId)
        {
            var claims = User.Claims;
            var id     = claims.FirstOrDefault(x => x.Type == "id")?.Value;

            if (id != userId.ToString())
            {
                return(Unauthorized());
            }

            using (var unit = _factory.GetUOF())
            {
                var dbFavorites = unit.UserFavorites.GetFavorites(userId);

                var list = new List <DTOSimpleUser>();
                foreach (var favorite in dbFavorites)
                {
                    var dtoSimpleUser = new DTOSimpleUser();
                    Mapper.Map(favorite.Expert, dtoSimpleUser);
                    list.Add(dtoSimpleUser);
                }

                return(list);
            }
        }
Esempio n. 2
0
        public IActionResult Get(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())
            {
                var dbUser = unit.Users.GetEager(id);
                var user   = new DTOUser
                {
                    InterestTags = new List <string>(),
                    Favorites    = new List <DTOSimpleUser>()
                };
                Mapper.Map(dbUser, user);

                foreach (var interestTag in dbUser.InterestTags)
                {
                    user.InterestTags.Add(interestTag.Tag.Name);
                }

                foreach (var favorite in dbUser.Favorites)
                {
                    var dtoSimpleUser = new DTOSimpleUser();
                    Mapper.Map(favorite.Expert, dtoSimpleUser);
                }

                return(Ok(user));
            }
        }
Esempio n. 3
0
        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;
                }
            }
        }
Esempio n. 5
0
        public ActionResult <DTOSimpleUser> AddFavorite([FromBody] DTOId expertId, Guid userId)
        {
            var claims = User.Claims;
            var id     = claims.FirstOrDefault(x => x.Type == "id")?.Value;

            if (id != userId.ToString())
            {
                return(Unauthorized());
            }

            using (var unit = _factory.GetUOF())
            {
                var dbUser   = unit.Users.GetEager(userId);
                var dbExpert = unit.Experts.Get(expertId.Id);

                if (dbExpert == null)
                {
                    return(BadRequest(new { message = $"Expert with id: {expertId} does not exist" }));
                }

                if (dbUser.Favorites.Any(x => x.ExpertId == expertId.Id))
                {
                    return(BadRequest(new { message = $"Favorite with id {expertId.Id}, is already in list" }));
                }

                var userFavorite = new UserFavorites
                {
                    Expert = dbExpert,
                    User   = dbUser
                };

                dbUser.Favorites.Add(userFavorite);

                unit.Complete();

                var simpleUser = new DTOSimpleUser();
                Mapper.Map(dbUser, simpleUser);
                return(CreatedAtAction("GetFavorites", simpleUser));
            }
        }
Esempio n. 6
0
        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;
                }
            }
        }
Esempio n. 7
0
        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;
                }
            }
        }