Ejemplo n.º 1
0
        public async Task <EventForCreateDto> UploadImage(int id, IFormFile image)
        {
            var dbEvent = await _context.Events.FindAsync(id);

            var uploadResult = await _cloudinaryService.UploadImage(image, "event-images", dbEvent.ImageId);

            dbEvent.ImageId  = uploadResult.PublicId;
            dbEvent.ImageUrl = uploadResult.Uri.ToString();

            await _context.SaveChangesAsync();

            return(EventTranslator.ToEventForCreateDto(dbEvent));
        }
Ejemplo n.º 2
0
        public async Task <EventForCreateDto> CreateEvent(EventForCreateDto ev)
        {
            var newEvent = new Event()
            {
                Title       = ev.Title,
                Description = ev.Description,
                Location    = ev.Location,
                StartDate   = new DateTime(ev.StartDate.Year, ev.StartDate.Month,
                                           ev.StartDate.Day, ev.StartTime.Hour, ev.StartTime.Minute, 0),
                EndDate = new DateTime(ev.EndDate.Year, ev.EndDate.Month,
                                       ev.EndDate.Day, ev.EndTime.Hour, ev.EndTime.Minute, 0),
                CreateDate = ev.CreateDate,
                CreatorId  = ev.CreatorId
            };

            _context.Events.Add(newEvent);

            var eventParticipants = new List <EventParticipant>()
            {
                new EventParticipant {
                    EventId = ev.Id, UserId = ev.CreatorId, Status = "accepted"
                }
            };

            if (ev.Offices != null)
            {
                foreach (var office in ev.Offices)
                {
                    var usersInOffice = await _context.Users.Include(u => u.Office)
                                        .Where(u => u.OfficeId == office.Id && u.Id != ev.CreatorId && u.Name != "admin").ToListAsync();

                    eventParticipants.AddRange(usersInOffice.Select(u =>
                                                                    new EventParticipant {
                        EventId = ev.Id, UserId = u.Id
                    }).ToList());
                }
            }

            if (ev.Users != null)
            {
                foreach (var user in ev.Users)
                {
                    //Add user to the eventParticipants list if it does not already exist
                    if (!eventParticipants.Exists(ep => ep.EventId == ev.Id && ep.UserId == user.Id))
                    {
                        eventParticipants.Add(new EventParticipant {
                            EventId = ev.Id, UserId = user.Id
                        });
                    }
                }
            }

            _context.EventParticipants.AddRange(eventParticipants);

            await _context.SaveChangesAsync();

            var attendees = await _context.EventParticipants.Include(ep => ep.User)
                            .Where(ep => ep.EventId == newEvent.Id).Select(u => u.User).ToListAsync();

            //Create a google calendar event and returns Google Event Id
            var googleEventId = await _googleCalendarService.CreateGoogleEvent(newEvent, attendees);

            newEvent.GoogleEventId = googleEventId;

            await _context.SaveChangesAsync();

            return(EventTranslator.ToEventForCreateDto(newEvent));
        }