source: FarmatikoData/FarmatikoRepo/PHRepo.cs@ afefe75

Last change on this file since afefe75 was db484c9, checked in by DimitarSlezenkovski <dslezenkovski@…>, 4 years ago

Fix bugs

  • Property mode set to 100644
File size: 5.3 KB
Line 
1using FarmatikoData.FarmatikoRepoInterfaces;
2using FarmatikoData.Models;
3using Microsoft.EntityFrameworkCore;
4using System.Collections.Generic;
5using System.Linq;
6using System.Threading.Tasks;
7
8namespace FarmatikoData.FarmatikoRepo
9{
10 public class PHRepo : IPHRepo
11 {
12 private readonly FarmatikoDataContext _context;
13 public PHRepo(FarmatikoDataContext context)
14 {
15 _context = context;
16 }
17 //GET
18 public async Task<PharmacyHead> GetPharmacyHeadByIdAsync(int id)
19 {
20 var Phead = await _context.PharmacyHeads.Where(x => x.Id == id).FirstOrDefaultAsync();
21 return Phead;
22 }
23
24 public async Task<IEnumerable<PharmacyHead>> GetPharmacyHeadInfo()
25 {
26 var PHeadInfo = await _context.PharmacyHeads.Take(10)
27 .Where(x => x.DeletedOn == null)
28 .Select(x => new PharmacyHead
29 {
30 Id = x.Id,
31 Name = x.Name,
32 Email = x.Email,
33 Password = x.Password,
34 Pharmacies = x.Pharmacies,
35 Medicines = x.Medicines
36
37 }).ToListAsync();
38 return PHeadInfo;
39 }
40 //POST
41 public async Task UpdatePharmacyHead(PharmacyHead pharmacyHead)
42 {
43 var Phead = await _context.PharmacyHeads.Where(x => x.Email == pharmacyHead.Email).FirstOrDefaultAsync();
44 var EditedPHead = await _context.PharmacyHeads.AsNoTracking<PharmacyHead>().Where(x => x.Email == pharmacyHead.Email).FirstOrDefaultAsync();
45
46 if (!EditedPHead.Email.Equals(pharmacyHead.Email))
47 EditedPHead.Email = pharmacyHead.Email;
48
49 if (!EditedPHead.Name.Equals(pharmacyHead.Name))
50 EditedPHead.Name = pharmacyHead.Name;
51
52 if (!EditedPHead.Password.Equals(pharmacyHead.Password))
53 EditedPHead.Password = pharmacyHead.Password;
54
55 if (!EditedPHead.Pharmacies.Equals(pharmacyHead.Pharmacies))
56 EditedPHead.Pharmacies = pharmacyHead.Pharmacies;
57
58 if (!EditedPHead.Medicines.Equals(pharmacyHead.Medicines))
59 EditedPHead.Medicines = pharmacyHead.Medicines;
60
61 await _context.SaveChangesAsync();
62 }
63 public async Task ClaimPharmacy(RequestPharmacyHead pharmacy)
64 {
65 await _context.PHRequests.AddAsync(pharmacy);
66 await _context.SaveChangesAsync();
67 }
68 public async Task Add(PharmacyHead pharmacyHead)
69 {
70 await _context.PharmacyHeads.AddAsync(pharmacyHead);
71 await _context.SaveChangesAsync();
72 }
73
74 public async Task Remove(PharmacyHead phead)
75 {
76 var Phead = await _context.PharmacyHeads.Where(x => x.Id == phead.Id).FirstOrDefaultAsync();
77 Phead.DeletedOn = phead.DeletedOn;
78 await _context.SaveChangesAsync();
79 }
80
81 public async Task RemoveClaimingRequest(int id)
82 {
83 var req = await _context.PHRequests.Where(r => r.Id == id).FirstOrDefaultAsync();
84 _context.PHRequests.Remove(req);
85 await _context.SaveChangesAsync();
86 }
87
88
89 public PharmacyHead GetPharmacyHeadByUserName(string userName)
90 {
91
92
93 var PHead = _context.PharmacyHeads
94 .Where(x => x.Email.Equals(userName))
95 .Select(x => new PharmacyHead
96 {
97 Email = x.Email,
98 Name = x.Name,
99 Password = x.Password,
100 Medicines = x.Medicines,
101 Pharmacies = x.Pharmacies
102 }).FirstOrDefault();
103
104
105
106 return PHead;
107 }
108
109 public List<PharmacyHeadMedicine> GetPharmacyHeadMedicines(string email)
110 {
111 var Phead = _context.PharmacyHeads.Where(x => x.Email.Equals(email)).FirstOrDefault();
112 var Medicines = _context.PharmacyHeadMedicines.Select(x => new PharmacyHeadMedicine
113 {
114 PheadId = x.PheadId,
115 Head = x.Head,
116 MedicineId = x.MedicineId,
117 Medicine = x.Medicine
118 }).ToList();
119 if (Medicines == null || Medicines == default)
120 Medicines = null;
121 var meds = Medicines.Where(x => x.PheadId == Phead.Id).ToList();
122
123 return meds;
124 }
125
126 public IEnumerable<PharmacyHead> GetPharmacyHeads()
127 {
128 var heads = _context.PharmacyHeads.ToList();
129 return heads;
130 }
131
132 public PharmacyHead GetPharmacyHead(string head)
133 {
134 var phead = _context.PharmacyHeads.Where(x => x.Email.Equals(head)).FirstOrDefault();
135 return phead;
136 }
137
138 public List<Pharmacy> GetPharmacies()
139 {
140 var pharms = _context.Pharmacies.ToList();
141 return pharms;
142 }
143
144 public void DeletePHMedicine(int id, int phId, int medId)
145 {
146 var PH = _context.PharmacyHeadMedicines.Where(x => x.PheadId == phId).Single();
147 var PHMed = _context.PharmacyHeadMedicines.Where(x => x.MedicineId == medId).Single();
148 var PHID = _context.PharmacyHeadMedicines.Where(x => x.Id == id).Single();
149 _context.PharmacyHeadMedicines.Remove(PHID);
150 _context.SaveChanges();
151 }
152 }
153}
Note: See TracBrowser for help on using the repository browser.