コード例 #1
0
        public async Task <IActionResult> AssignParticipantToRoom([FromRoute] int roomId,
                                                                  [FromBody] RoomAssignmentBody body)
        {
            if (!_context.Room.Any(r => r.id == roomId))
            {
                return(NotFound(new BasicResponse {
                    message = "Nie znaleziono pokoju o podanym id"
                }));
            }

            if (body.mailAddress != null)
            {
                if (!_context.User.Any(u => u.mailAddress.Equals(body.mailAddress)))
                {
                    return(NotFound(new BasicResponse
                    {
                        message = "Nie znaleziono użytkownika o podanym adresie e-mail"
                    }));
                }
                var fetchedRoom = _context.Room.First(r => r.id == roomId);
                if (fetchedRoom.hostMailAddress != null)
                {
                    return(BadRequest(new BasicResponse {
                        message = "Host already assigned"
                    }));
                }

                var roomParticipant = new RoomParticipant()
                {
                    mailAddress = body.mailAddress, roomId = roomId
                };
                _context.RoomParticipant.Add(roomParticipant);
                await _context.SaveChangesAsync();

                return(NoContent());
            }

            if (body.username != null)
            {
                var roomParticipant = new RoomParticipant()
                {
                    userName = body.username, roomId = roomId
                };
                _context.RoomParticipant.Add(roomParticipant);
                await _context.SaveChangesAsync();

                return(NoContent());
            }

            return(BadRequest(new BasicResponse()
            {
                message = "Missing parameters"
            }));
        }
コード例 #2
0
        public async Task <IActionResult> AssignPoToRoom([FromRoute] int roomId, [FromBody] RoomAssignmentBody body)
        {
            if (!_context.Room.Any(r => r.id == roomId))
            {
                return(NotFound(new BasicResponse {
                    message = "Nie znaleziono pokoju o podanym id"
                }));
            }

            var fetchedRoom = _context.Room.First(r => r.id == roomId);

            if (fetchedRoom.hostMailAddress != null)
            {
                return(BadRequest(new BasicResponse {
                    message = "Host already assigned"
                }));
            }

            if (body.mailAddress != null)
            {
                if (!_context.User.Any(u => u.mailAddress.Equals(body.mailAddress)))
                {
                    return(NotFound(new BasicResponse
                    {
                        message = "Nie znaleziono użytkownika o podanym adresie e-mail"
                    }));
                }
                fetchedRoom.hostMailAddress = body.mailAddress;
                _context.Entry(fetchedRoom).Property(t => t.hostMailAddress).IsModified = true;
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            if (body.username != null)
            {
                fetchedRoom.hostUsername = body.username;
                _context.Room.Attach(fetchedRoom);
                _context.Entry(fetchedRoom).Property(t => t.hostUsername).IsModified = true;
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            return(BadRequest(new BasicResponse()
            {
                message = "Missing parameters"
            }));
        }