summaryrefslogtreecommitdiff
path: root/Profiles/AutomapperProfiles.cs
blob: 8d6978883cff5a44a0db5b457d154c1e8758841c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using AutoMapper;
using LibraryAPI.Models;
using LibraryAPI.DTOs;

namespace LibraryAPI.Profiles {
    public class AutomapperProfiles : Profile {
        public AutomapperProfiles() {
            CreateMap<CreateUserAccount, UserAccount>();
            CreateMap<UserAccount, UserAccountDTO>().ForMember(dto => dto.Groups, o => o.MapFrom(UserDTOMap));
            //CreateMap<UserAccountDTO, UserAccount>();
            CreateMap<CreateGroup, Group>();
            CreateMap<Group, GroupDTO>().ReverseMap();
        }

        private List<GroupDTO> UserDTOMap(UserAccount user, UserAccountDTO dto) {
            List<GroupDTO> groups = new List<GroupDTO>();

            if(user.Groups == null)
                return groups;

            foreach (var g in user.Groups) {
                groups.Add(new GroupDTO {
                    Id = g.GroupId,
                    Name = g.Group.Name
                });
            }

            return groups;
        }
    }
}