Esempio n. 1
0
        public IActionResult Get(int id)
        {
            try
            {
                var inv = _repo.GetInvitation(id);
                if (inv == null)
                {
                    return(NotFound());
                }

                var model = Mapper.Map <InvitationModel>(inv);
                model.Inviter = GetUserModel(inv.InviterId);
                model.Invitee = GetUserModel(inv.InviteeId);
                model.Event   = GetEventModel(inv.EventId);

                return(Ok(model));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Exception thrown while fetching event: {ex}");
            }

            return(BadRequest("Could not fetch event"));
        }
Esempio n. 2
0
        public async Task <IActionResult> Put(int id,
                                              [RequiredFromQuery] string insert,
                                              [RequiredFromQuery] string inv)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (!int.TryParse(insert, out int inviteeId))
                {
                    return(BadRequest("Invalid invitee id"));
                }

                if (!int.TryParse(inv, out int invitationId))
                {
                    return(BadRequest("Invalid invitation id"));
                }

                var seekedInvitee = _repo.GetUser(inviteeId);
                if (seekedInvitee == null)
                {
                    _logger.LogWarning($"User which which wants to be an invitee is not registered in the database");
                    return(Unauthorized());
                }

                var seekedInvitation = _repo.GetInvitation(invitationId);
                if (seekedInvitation == null)
                {
                    _logger.LogWarning($"Invitation with id {invitationId} not found in the database.");
                    return(NotFound());
                }

                if (seekedInvitee.Id != seekedInvitation.InviteeId)
                {
                    _logger.LogWarning($"User is not allowed to accept other's person invitation.");
                    return(Unauthorized());
                }

                var ev = _repo.GetEvent(id);
                if (ev == null)
                {
                    return(NotFound());
                }

                String inviteeIdentifier = inviteeId.ToString();
                String attendeesIds      = ev.AttendeesIds;
                if (attendeesIds.Contains(";" + inviteeIdentifier + ";"))
                {
                    _logger.LogWarning($"User registered on the event earlier.");
                    return(BadRequest("Could not update event attendees"));
                }

                StringBuilder sb = new StringBuilder();
                sb.Append(attendeesIds).Append(inviteeIdentifier).Append(";");

                ev.AttendeesIds = sb.ToString();

                _repo.Update(ev);
                if (await _repo.SaveAllAsync())
                {
                    _repo.Delete(seekedInvitation);
                    if (await _repo.SaveAllAsync())
                    {
                        EventModel mapped = Mapper.Map <EventModel>(ev);
                        mapped.Host = GetHostModel(ev);
                        return(Ok(mapped));
                    }
                    else
                    {
                        _logger.LogError($"Attendee added but invitations not deleted.");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Exception thrown while updating event attendees: {ex}");
            }

            return(BadRequest("Could not update event attendees"));
        }