|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using PortfolioApi.Models.Interfaces.Repos; |
| 4 | +using PortfolioApi.Models.Profiles; |
| 5 | +using PortfolioApi.Repository.EntityFramework.Context; |
| 6 | + |
| 7 | +namespace PortfolioApi.Core.Domains.Profiles.Repository |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// The Repository Implementation for Profile Entities |
| 11 | + /// </summary> |
| 12 | + public class ProfilesEntityRepository : IRepoCrud<Profile> |
| 13 | + { |
| 14 | + private readonly PortfolioContext _portfolioContext; |
| 15 | + |
| 16 | + public ProfilesEntityRepository(PortfolioContext portfolioContext) |
| 17 | + { |
| 18 | + _portfolioContext = portfolioContext; |
| 19 | + } |
| 20 | + |
| 21 | + public Profile Create(Profile input) |
| 22 | + { |
| 23 | + var changeTracking = _portfolioContext.Profiles.Add(input); |
| 24 | + var result = _portfolioContext.SaveChanges(); |
| 25 | + return input; |
| 26 | + } |
| 27 | + |
| 28 | + public Profile Delete(Profile input) |
| 29 | + { |
| 30 | + var model = _portfolioContext.Profiles.Find(input.Id); |
| 31 | + _portfolioContext.Remove(model); |
| 32 | + _portfolioContext.SaveChanges(); |
| 33 | + return model; |
| 34 | + } |
| 35 | + |
| 36 | + public IEnumerable<Profile> Get(Profile input) |
| 37 | + { |
| 38 | + if (input.Info != null && !string.IsNullOrEmpty(input.Info.LastName) && !string.IsNullOrEmpty(input.Info.FirstName)) |
| 39 | + { |
| 40 | + return _portfolioContext.Profiles.Where( |
| 41 | + x => x.Info.LastName == input.Info.LastName |
| 42 | + && x.Info.FirstName == input.Info.FirstName); |
| 43 | + } |
| 44 | + else if (input.Info != null && !string.IsNullOrEmpty(input.Info.LastName)) |
| 45 | + { |
| 46 | + return _portfolioContext.Profiles.Where(x => x.Info.LastName == input.Info.LastName); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + var result = new List<Profile>(); |
| 51 | + var item = _portfolioContext.Profiles.Find(input.Id); |
| 52 | + if(item != null) result.Add(item); |
| 53 | + return result; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public Profile Update(Profile input) |
| 58 | + { |
| 59 | + var m = _portfolioContext.Profiles.Find(input.Id); |
| 60 | + m.Info = input.Info; |
| 61 | + var result = _portfolioContext.SaveChanges(); |
| 62 | + return m; |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments