Esempio n. 1
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. 2
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;
                }
            }
        }