Skip to content

Q & A ๐Ÿ‹โ€โ™‚

KOSEUNGBIN edited this page Sep 15, 2020 · 2 revisions

08-23

  • backend ์„œ๋ฒ„ ์‹คํ–‰ ํ›„ frontend ์„œ๋ฒ„๋ฅผ ๋„์› ๋Š”๋ฐ backend ๋กœ ์š”์ฒญ์ด ์•ˆ๋„˜์–ด ๊ฐ€๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
  • ์›์ธ: mock server๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด์„œ์ž„ .env.developemnt ์—์„œ REACT_APP_USE_API_SERVER ๊ฐ’์„ true ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์‹ค์ œ backend ์„œ๋ฒ„๋กœ ์š”์ฒญ ๊ฐ

08-18

PR review

java์—์„œ collection์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•๋“ค

enhanced for-loop (ํ–ฅ์ƒ๋œ for๋ฌธ)

  • java5 ๋ถ€ํ„ฐ ๋„์ž…
  • ๋ฐฐ์—ด or ๋ฆฌ์ŠคํŠธ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ
  • ์ฝ๊ธฐ์ „์šฉ
List<Room> rooms = roomService.getRooms();
for (Room room : rooms) {
		RoomResponse roomResponse = new RoomResponse();
		roomResponse.setId(room.getId());
		roomResponse.setTitle((room.getTitle()));
		roomResponseList.add(roomResponse);
}

Iterable.forEach

  • java8 ๋ถ€ํ„ฐ ๋„์ž…
  • List ์ธํ„ฐํŽ˜์ด์Šค > Collection ์ธํ„ฐํŽ˜์ด์Šค > Iterable ์ธํ„ฐํŽ˜์ด์Šค (forEach)
List<Room> rooms = roomService.getRooms();
rooms.forEach(room -> {
		RoomResponse roomResponse = new RoomResponse();
		roomResponse.setId(room.getId());
		roomResponse.setTitle(room.getTitle());
		roomResponseList.add(roomResponse);
});

Stream.forEach

  • collection ์„ stream์œผ๋กœ ๋ณ€ํ™˜ํ•œ ๋’ค stream ์˜ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ๊ฐ€๋Šฅ(forEach, map, filter ๋“ฑ)
List<Room> rooms = roomService.getRooms();
List<RoomResponse> roomResponses = rooms.stream()
			.map(room -> {
				RoomResponse roomResponse = new RoomResponse();
				roomResponse.setId(room.getId());
				roomResponse.setTitle(room.getTitle());
				return roomResponse;
			})
			.collect(Collectors.toList());
List<Room> rooms = roomService.getRooms();
List<RoomResponse> roomResponses = rooms.stream()
			.map(RoomResponse::create) // method reference
			.collect(Collectors.toList());

//---------------------
@Getter
@Setter
public class RoomResponse {
	private Long id;
	private String title;

	public static RoomResponse create(Room room) {
		RoomResponse roomResponse = new RoomResponse();
		roomResponse.setId(room.getId());
		roomResponse.setTitle(room.getTitle());
		return roomResponse;
	}
}

์ฐธ๊ณ ์ž๋ฃŒ

08-16

๊ถŒ์˜ˆ๊ฒฝ

  • ๋ฐฑ์—”๋“œ ์„œ๋ฒ„ ํ”„๋กœ์ ํŠธ์—์„œ Controller์˜ Request/Response ๊ฐ์ฒด์˜ ์—ญํ• ์€ ๋ฌด์—‡์ธ๊ฐ€?

    ์š”์ฒญ, ์‘๋‹ต body๊ฐ€ ํ•ญ์ƒ ์—”ํ‹ฐํ‹ฐ์™€ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค.

    ์š”์ฒญ, ์‘๋‹ต์œผ๋กœ ์ธํ•ด ์—”ํ‹ฐํ‹ฐ์˜ ๋ชจ๋ธ๋ง์ด ๊นจ์ง€๋ฏ€๋กœ Side Effect๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์—”ํ‹ฐํ‹ฐ๋Š” Service ๋กœ์ง์—์„œ๋งŒ ์‚ฌ์šฉ๋œ๋‹ค.

    JSON ์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ Object๋กœ Request/Response ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

    ๋ฌผ๋ก  Map<key, value> ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์•ˆํ‹ฐ ํŒจํ„ด์ด๋‹ค.

    image

    Map์„ ๋ฌด๋ถ„๋ณ„ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, ์œ ์ง€ ๋ณด์ˆ˜ ์–ด๋ ค์›€ ๊ฒช๊ฒŒ ๋œ๋‹ค.

    ์—”ํ‹ฐํ‹ฐ๋ฅผ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•˜๋ฉด, ์‘๋‹ต ๋ฐ์ดํ„ฐ์˜ ๋ณ€๊ฒฝ์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊นŒ์ง€ ์˜ํ–ฅ์„ ๋ฏธ์น˜๊ฒŒ ๋œ๋‹ค.

    ์—”ํ‹ฐํ‹ฐ์™€ ๋ถ„๋ฆฌํ•˜๋ฉด, Request/Response ๊ฐ์ฒด๋Š” ์—ญํ• ์— ๋งž๊ฒŒ **์š”์ฒญ/์‘๋‹ต ๋ฐ์ดํ„ฐ ๊ฒ€์ฆ(Validation)**์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

    image

  • ์•„ํ† ๋ฏน ๋””์ž์ธ ์ชผ๊ฐœ๋Š” ๊ธฐ์ค€?

    components ๋””๋ ‰ํ† ๋ฆฌ๋Š” ์ตœ๋Œ€ํ•œ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๊ณ ๋ คํ•ด์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉด ์ข‹๋‹ค.

    ๋˜ํ•œ components ์•ˆ์—์„œ๋„ atoms, molecules, organisms ๋กœ ๋‚˜๋‰˜๋Š”๋ฐ, atoms โ†’ molecules โ†’ organisms ์ˆœ์œผ๋กœ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์ด์šฉํ•ด์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด, molecules ์˜ Message ์ปดํฌ๋„ŒํŠธ๋Š” atoms ์˜ Text ์ปดํฌ๋„ŒํŠธ ๊ทธ๋ฃน์œผ๋กœ '๋‚ด์šฉ'๊ณผ '๋“ฑ๋ก ๋‚ ์งœ'๋ฅผ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

    <StyledMessage who={who}>
        <TextMessage>
            <Text text={contents} size="md" boxModel="sm" />
            <Text text={createdAt} size="sm" boxModel="sm" />
        </TextMessage>
    </StyledMessage>

๊น€์•„์œค

  • Mock ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ ๋ฐฉ์‹์ด ๋ฌด์—‡์ธ๊ฐ€?

    ํ”„๋ก ํŠธ์˜ Mock ๋ฐ์ดํ„ฐ๋Š” ์‹ค์ œ๋กœ Mock Server๋ฅผ ์ด์šฉํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.

    ์„œ๋ฒ„์ชฝ ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ์ด ์•„์ง ์ด๋ฃจ์–ด์ง€์ง€ ์•Š์€ ์ƒํ™ฉ์—์„œ, ํ”„๋ก ํŠธ์—์„œ๋Š” ์ •ํ•ด ๋†“์€ API ์ŠคํŽ™ ๊ธฐ๋ฐ˜์œผ๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ Mock ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

    Axios ๋ชจ๋“ˆ์€ ์„œ๋ฒ„์™€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๊ธฐ ์œ„ํ•œ HTTP Client ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

    ํ•˜์ง€๋งŒ ์•„์ง ์„œ๋ฒ„์— ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ์ด ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, ์„œ๋ฒ„๋กœ์˜ ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„์„œ Mock(์ž„์‹œ/๋Œ€์ฒด) ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ๋งŒ ํ•œ๋‹ค.

    ์ด๋•Œ, Axios ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•œ ์š”์ฒญ์„ axios-mock-adapter ์ด์šฉํ•ด์„œ ์ž„์‹œ(Mock) ๋ฐ์ดํ„ฐ๋กœ ์‘๋‹ตํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

    function getRooms(mock) {
        mock.onGet('/api/rooms')
            .reply(200, {
                rooms: [{ id: 1, text: 'hello world' }, { id: 2, text: 'hi' }]
            });
    }
    
    function createRooms(mock) {
        mock.onPost('/api/rooms')
            .reply(201, {
                id: 23, text: 'good'
            });
    }
    
    export default {
        getRooms, createRooms
    };

    ์œ„์™€ ๊ฐ™์ด, ์ธ์ž๋ฅผ ์ „๋‹ฌ๋ฐ›์€ mock ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž„์˜๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

    ๊ณ ์Šน๋นˆ

    • Response ๊ฐ์ฒด๋กœ์˜ ๋ณ€ํ˜•์„ Controller, Service ์ค‘ ์–ด๋””์„œ ํ•˜๋Š”๊ฒŒ ๋งž์„๊นŒ?

      Controller์—์„œ ์ฒ˜๋ฆฌํ•˜๋ฉด ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์—”ํ‹ฐํ‹ฐ์— ๋Œ€ํ•œ ์˜์กด์„ฑ์ด ์ƒ๊ธฐ๊ณ , Service์—์„œ ์ฒ˜๋ฆฌํ•˜๋ฉด ์„œ๋น„์Šค๊ฐ€ Response์— ๋Œ€ํ•œ ์˜์กด์„ฑ์ด ์ƒ๊ธด๋‹ค.

      ํ•˜์ง€๋งŒ, ์š”์ฒญ/์‘๋‹ต์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋Š” ์ปจํŠธ๋กค๋Ÿฌ์˜ ์ฑ…์ž„์ด๊ธฐ ๋•Œ๋ฌธ์— Contoller์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒŒ ๋งž์„ ๊ฑฐ ๊ฐ™๋‹ค.

      ๋˜ํ•œ, Message โ†’ MessageResponse ๋กœ์˜ ๋ณ€ํ˜•์„ MessageResponse ๋‚ด๋ถ€์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์ข€ ๋” ๊น”๋”ํ•œ ์ฝ”๋“œ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. (๋ฌผ๋ก  ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ฐœ ์Šคํƒ€์ผ์— ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.)

    • ์ผ๋Œ€๋‹ค ๊ด€๊ณ„์—์„œ ์—ฐ๊ด€ ๊ด€๊ณ„๋ฅผ ์–ด๋””์— ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ ํ•ฉํ• ๊นŒ?

      ์ฑ„ํŒ…๋ฐฉ ์ž…์žฅ์‹œ ๋ฉ”์‹œ์ง€ ๋ฆฌ์ŠคํŠธ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ ยท Issue #7 ยท LandvibeDev/web-chat-backend