source: FarmatikoData/FarmatikoRepo/PHRepo.cs@ 68454c6

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

fix bugs

  • Property mode set to 100644
File size: 4.9 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).Where(x => x.DeletedOn == null)
27 .Select(x => new PharmacyHead
28 {
29 Id = x.Id,
30 Name = x.Name,
31 Email = x.Email,
32 Password = x.Password,
33 MedicineList = x.MedicineList,
34 PharmaciesList = x.PharmaciesList
35 }).ToListAsync();
36 return PHeadInfo;
37 }
38 //POST
39 public async Task UpdatePharmacyHead(PharmacyHead pharmacyHead)
40 {
41 var Phead = await _context.PharmacyHeads.Where(x => x.Email == pharmacyHead.Email).FirstOrDefaultAsync();
42 var EditedPHead = await _context.PharmacyHeads.AsNoTracking<PharmacyHead>().Where(x => x.Email == pharmacyHead.Email).FirstOrDefaultAsync();
43 EditedPHead.Email = pharmacyHead.Email;
44 EditedPHead.Name = pharmacyHead.Name;
45 EditedPHead.Password = pharmacyHead.Password;
46 /*if (pharmacyHead.MedicineList.Count() == 0)
47 pharmacyHead.MedicineList = null;*/
48 EditedPHead.MedicineList = pharmacyHead.MedicineList;
49 EditedPHead.PharmaciesList = pharmacyHead.PharmaciesList;
50 EditedPHead.PHMedicineList = pharmacyHead.PHMedicineList;
51 //_context.Entry<PharmacyHead>(Phead).State = EntityState.Detached;
52 Phead = EditedPHead;
53 await _context.SaveChangesAsync();
54 }
55 public async Task ClaimPharmacy(RequestPharmacyHead pharmacy)
56 {
57 await _context.PHRequests.AddAsync(pharmacy);
58 await _context.SaveChangesAsync();
59 }
60 public async Task Add(PharmacyHead pharmacyHead)
61 {
62 await _context.PharmacyHeads.AddAsync(pharmacyHead);
63 await _context.SaveChangesAsync();
64 }
65
66 public async Task Remove(PharmacyHead phead)
67 {
68 var Phead = await _context.PharmacyHeads.Where(x => x.Id == phead.Id).FirstOrDefaultAsync();
69 Phead.DeletedOn = phead.DeletedOn;
70 await _context.SaveChangesAsync();
71 }
72
73 public async Task RemoveClaimingRequest(int id)
74 {
75 var req = await _context.PHRequests.Where(r => r.Id == id).FirstOrDefaultAsync();
76 _context.PHRequests.Remove(req);
77 await _context.SaveChangesAsync();
78 }
79
80
81 public PharmacyHead GetPharmacyHeadByUserName(string userName)
82 {
83 var PHead = _context.PharmacyHeads
84 .Where(x => x.Email.Equals(userName))
85 .FirstOrDefault();
86
87 return PHead;
88 }
89
90 public List<PharmacyHeadMedicine> GetPharmacyHeadMedicines(string email)
91 {
92 /*var meds = _context.Medicines.ToList();
93 var medicines = Medicines;*/
94 var Phead = _context.PharmacyHeads.Where(x => x.Email.Equals(email)).FirstOrDefault();
95 var Medicines = _context.PharmacyHeadMedicines.Where(x => x.PheadId == Phead.Id).ToList();
96 /*.Select(x => x.Head.MedicineList)
97 .SelectMany(mList => mList)
98 .ToList();*/
99
100
101 return Medicines;
102 }
103
104 public IEnumerable<PharmacyHead> GetPharmacyHeads()
105 {
106 var heads = _context.PharmacyHeads.ToList();
107 return heads;
108 }
109
110 public PharmacyHead GetPharmacyHead(string head)
111 {
112 var phead = _context.PharmacyHeads.Where(x => x.Email.Equals(head)).FirstOrDefault();
113 return phead;
114 }
115
116 public List<Pharmacy> GetPharmacies()
117 {
118 var pharms = _context.Pharmacies.ToList();
119 return pharms;
120 }
121
122 public void DeletePHMedicine(int id, int phId, int medId)
123 {
124 var PH = _context.PharmacyHeadMedicines.Where(x => x.PheadId == phId).Single();
125 var PHMed = _context.PharmacyHeadMedicines.Where(x => x.MedicineId == medId).Single();
126 var PHID = _context.PharmacyHeadMedicines.Where(x => x.Id == id).Single();
127 _context.PharmacyHeadMedicines.Remove(PHID);
128 _context.SaveChanges();
129 }
130 }
131}
Note: See TracBrowser for help on using the repository browser.